I’m doing something wrong here.
I’m trying to access a script called GUIManager sitting on a game object called player from another game object called sign.
In sign i have:
void OnMouseDown(){
Debug.Log("hit");
guiManager = (GUIManager)GameObject.Find("PlayerPlaceHolder").GetComponent("GUIManager");
guiManager.showWindow = !guiManager.showWindow;
}
This causes a nullReferenceException at runtime.
What am i doing wrong?
GUIManager GM = myObject.GetComponent(typeof(GUIManager)) as GUIManager;
i do it like this.
If you’re using C#, you can employ generics and avoid having to do a runtime cast. And since your error is talking about a null reference, let’s do some checks to see where the problem is:
GameObject playerPlaceHolder = GameObject.Find("PlayerPlaceHolder");
if (playerPlaceHolder == null)
{
Debug.Log("PlayerPlaceHolder could not be found!");
}
else
{
GUIManager guiManager = playerPlaceHolder.GetComponent<GUIManager>();
if (guiManager == null)
{
Debug.Log("GUIManager component could not be found!");
}
else
{
guiManager.showWindow = !guiManager.showWindow;
}
}
legend Zero_Quantum. cheers
thanks FizixMan, that’s much more robust as well.