So far I am not clear how do you actually access the various components on a game object, to enable and disable them.
I have a component that has various scripts and elements, like a camera, transform, audio listener and few other custom scripts.
It is in the scene, so I try to access to it via script:
private GameObject _player;
_player = GameObject.Find("the_player")
So far so good; if I want to access for example to the camera, and enable or disable it, I can do it
_player.GetComponent<Camera>().enabled = true;
This disable correctly the camera, or eanble it, depending from the paramenter that I pass.
Now, if I want to access my script, called "playerscript"; I can't do the same thing, since I get an error:
_player.GetComponent<playerscript>().enabled = true;
Not sure why I get an error; I have a class that is called "playerscript", that is attached to that _player object, but instead, VS complain.
This is the error message:
Error CS0246 The type or namespace name 'playerscript' could not be found (are you missing a using directive or an assembly reference?)
I did try to use also GetComponentInChildren, but the result is the same.
If I have the object _player, which hold a reference to the original game object instantiated in the scene, why I can't simply access all the elements attached to it, via GetComponent and dot notation? I believe this used to work time ago, but in Unity5 I can't get this to work.
EDIT: This is the content of the playerscript class:
using UnityEngine;
using System.Collections;
public class playerscript : MonoBehaviour
{
// Use this for initialization
void Start()
{
// get the instance of the button from UI manager
}
void OnTriggerEnter(Collider world_item)
{
//enable item animation
}
void OnTriggerExit(Collider world_item)
{
//disable item animation
}
}
playerscript
is in? – ZEKE Feb 15 at 11:39