- Home /
NullReferenceException (C#)
Getting "NullReferenceException: Object reference not set to an instance of an object UserInterfaceControllerScript.Start () (at Assets/Scripts/UserInterfaceControllerScript.cs:10)"
The code on line 10 is part of the Start () method on line 10, which says:4
void Start () {
cameraControlScript = gameObject.GetComponent<CameraControllerScript>();
cameraControlScript.isExtraActionMenuActive = false;
}
I initiated cameraControlScript as:
CameraControllerScript cameraControlScript;
in order to bring in a public bool that is in the CameraControllerScript. Am I going about this wrong? I just want to be able to access and change the public bool's status between true and false from inside this script.
Any ideas?
Is this attached to the same object as the CameraControlScript?
this isn't attached to anything yet. the only reason I know there's an error is because it's in the console before I hit play. I haven't built the UI to attach this script to xD
Answer by Demigiant · Dec 22, 2012 at 08:20 PM
This can only mean that the CameraControllerScript Component is not attached to the same GameObject where UserInterfaceControllerScript is attached to, since GetComponent didn't find it.
UserInterfaceControllerScript isn't attached to anything yet, though I don't see why it'd matter? $$anonymous$$ust I attach it in order for the public bool to communicate between the two scripts?
It needs to be attached to the same GameObject to which you attach CameraControllerScript. Not in order for the bool to communicate, but in order for gameObject.GetComponent to find CameraControllerScript, and thus its bool property
Your answer
Follow this Question
Related Questions
Unity event calling function gets nullreferencexception on bool 0 Answers
Accessing Instance Bool from other script 2 Answers
Problems accessing other script's variable 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers