First off, I’m new to unity and C#, so the chances are very high I’ve just missed something really basic, but after hours of searching, I can’t figure out why I get this error. All of the examples I see seem to have cubeScript cScript = cCube.GetComponent();
separate from any functions, but I can’t get it to work in anything other than the Start ()
function. Am I supposed to have it in Start ()
? And if so, how do I access it in the rest of the script? (Right now I get the “Does not exist in current context” error). My code is :
public class playerScript : MonoBehaviour {
public GameObject cCube;
cubeScript cScript = cCube.GetComponent<cubeScript>();
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (cScript.isColliding == true) {
//do stuff
}
}
}
And:
public class cubeScript : MonoBehaviour {
public bool isColliding = false;
void OnCollsionEnter(Collision col) {
if (col.gameObject != null) {
isColliding = true;
}
}
}