Hey guys, I am trying to use a private variable inside a public function that is used as the Button Click event, but it is not working.
Here is what I have it currently setup:
private GameObject myGameObj;
void Awake(){
myGameObj = GameObject.Find("game_object_name");
}
// This function works fine, myGameObject becomes inactive
void Start(){
myGameObj.SetActive(false)
}
public void myFunction(){
myGameObj.SetActive(true);
}
And in my UI Button, in the click event I am calling myFunction. I know the reference for myGameObj is correct because during the Start event, I am setting it to be inactive and that works just fine. But when I try to make it active again (using a button event) it does not work!
My first though was some issue related to the GameObject be inactive, but I removed the SetActive
code and tried to only print the reference using Debug.Log
.
Printing it during the Start event shows the right reference. Printing it during the myFunction event shows a null
reference.
Any ideas? Thank you!