C# Issues with Bool access on another script.

I have two scripts one with the my bool variable and the other script that I’m trying to get the bool state from.

—Script 1–

public class PlayerController : MonoBehaviour

public bool atCenter;

void Start()
{
       atCenter = false;
}

–Script 2–

public class CameraMover : MonoBehaviour

public PlayerController playerController;

void Start()  
     { 
           //I've don't this as playerController = GetComponent<PlayerController>(); and got the same results. 
		playerController = GameObject.Find ("PlayerHolder").GetComponent<PlayerController>();
	}

void Update()
  {
       if(playerController.atCenter)
		{
			StartCoroutine(TopDownZoomIn(zoomInTime));
		}
  }

Now I am able to change the bool state in script 2 with playerController.atCenter = false; but trying to get it’s state I get this error : NullReferenceException: Object reference not set to an instance of an object
CameraMover.Update () (at Assets/_Scripts/CameraMover.cs:30)

I’ve still very, very new to this.

BTW Script 1 is on an empty game object that is holding the camera, Script 2 is on an empty game object holding my player with the tag “PlayerHolder” . Both object are in the scene when started.

Thanks for the help!

What is line 30 of CameraMover.cs? It looks like you may have edited the code as there are only 17 lines

Sorry line 30 is line 13 of the shortened code in the question above.

I could post the whole code of each if that would help. But like I said I’m still new so it’s rather ugly :smile:

That sounds like playerController is null. Are you sure that the GameObject named “PlayerHolder” has the PlayerController behavior on it?
Actually, from your description, it sounds like CameraMover is the one on PlayerHolder, which would be why it’s not there.

Since you have a public PlayerController on line 2 of CameraMover, you could just drag the PlayerController game object right onto the CameraMover game object in the PlayerController field. This is done through the Unity editor itself. That way, you wouldn’t have to search for it in code, because I suspect your search is coming up with a null value for the playerController variable on line 8.

Thanks for the input. I think I have something a little out of wack in the code. I created a new project with a little simpler script and I had no problem accessing the bools and modifying them from another script using the methods above. So I think I need to go through my main scripts and find out what I have wrong.