I have a gameobject called Starting containing one Square and a legacy Text
The square serves as a button with the text saying "Press Start Button". The user will press the start button (mapped to spacebar). This will set the Starting gameObject to inactive. The code is as follows:
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
if (CharacterController.started == false)
{
CharacterController.started = true;
gameObject.SetActive(false);
}
else if (CharacterController.started == true)
{
CharacterController.started = false;
gameObject.SetActive(true);
}
}
}
CharacterController.started is a boolean for another gameObject.
I understand the GameObject.find() does not work if the gameObject has been set as inactive. I tried to find the GameObject from the CharacterController script like so:
void Update()
{
if (started)
{
if (Input.GetKeyDown(KeyCode.Space))
{
starting = GameObject.Find("Starting");
starting.SetActive(true);
}
}
}
This did not work and gives the error:
NullReferenceException: Object reference not set to an instance of an object CharacterController.Update () (at Assets/CharacterController.cs:33)
I am new to Unity. How do I get the Starting GameObject and set is as active?