Hello,
I am trying to make a block falling game in unity. In my scene I have two scripts, the first is called ‘GameBehaior’ and is attached to my GameController. The second is called ‘PillBehavior’ and is attached to the capsule gameobject at runtime. The GameBehavior script has a integer in it called counter. My PillBehavior script is trying to access the counter in the GameBehavior script. However when I try to use Debug.Log to print out the counter to check if it is working I get the following error “NullReferenceException: Object reference not set to an instance of an object”
The code that I am using to try to get the counter out of GameController in PillBehavior is this:
gamecontroller = GameObject.FindGameObjectWithTag ("GameController");
script = gamecontroller.GetComponent<GameBehavior> ();
Debug.Log (script.counter);
The debug line is the line that is giving me the NullReferenceError
Script and gamecontroller are defined like this:
GameObject gamecontroller;
public GameBehavior script;
Also, I dont know if this matters are not but counter is defined like this in the GameBehavior script:
public int counter; //number of pills on the screen