Hope you bear with me, I'm a beginner! Maybe I should've paid better attention to the tutorials but they all seem to use public variables/game objects that we can then drag into using the Unity Editor. However, if I have a private Game Object, how can I reference that programatically? I have the following:
private GameObject menuCanvas;
void Start()
{
menuCanvas = GameObject.Find("menuCanvas");
menuCanvas.SetActive(false);
}
Where the menuCanvas is named such in the Unity editor. However, I usually get the following error:
"The variable menuCanvas of myScript has not been assigned."
myScript is attached to a different GameObject in the scene, but not attached to menuCanvas. What am I doing wrong? Thanks for the help!
Edit: another question - which is generally better to use?
Edit 2: Hi guys, I figured it out. I had to move some code around to the Awake() function, so it looked like this:
private GameObject menuCanvas;
void Awake()
{
menuCanvas = GameObject.Find("menuCanvas");
}
void Start()
{
menuCanvas.SetActive(false);
}
And that worked. I realized that myScript was attached to multiple objects, which would cause the menuCanvas to be set inactive more than once (but it could not be referenced inactive).
S. Tarık Çetin's answer worked just as well! Thanks for the help
Edit 3: As Jesse Williams answered, an internal
definition keeps the variable hidden from the editor, but it can still be assigned via code.