- Home /
The variable has not been assigned even though it has been
Unity says that my button object has not been assigned even though it has. I am instantiating a canvas prefab for every player that has three buttons in it, I need to reference them so that I can make them disappear after they are clicked. In the photo I have clicked the play button.
playButton = canvas.transform.GetChild (2).gameObject;
blueButton = canvas.transform.GetChild (4).gameObject;
blueButton.SetActive (false);
redButton = canvas.transform.GetChild (3).gameObject;
redButton.SetActive (false);
print (playButton.name + blueButton.name + redButton.name);
Is the playButton public and visible in the Inspector? I bet it is not assigned in the Inspector
From the console output in the screenshot it can be seen that playButton
gets assigned, but the variable playButton
in playercontroller.play()
does not have a value. Are they the same variable? Do you try to set playButton
anywhere else (that might return null)? Are you trying to access the button variable on the server?
Answer by iveL_newO · May 03, 2018 at 03:35 AM
I fixed it.
I was instantiating the buttons and the player, the buttons were using onClick listeners from the player's script so I just had to assign the listeners via script. Thank you
Your answer
Follow this Question
Related Questions
When I Instantiate a prefab with a button it flies away? 1 Answer
Instantiate buttons for different actions 0 Answers
Button then instanitates gameobject 1 Answer
Instantiate object with button and place another object with mouse click 2 Answers
Scrolling Inventory - Instantiate Buttons Too Slow [UI 4.6] 2 Answers