malt
malt

Reputation: 15

Set child game object as active

So, I have a gameobject that is inactive at the start of the game. So, I put it inside a parent object that is active at the beginning of the game. The active at start parent object is Scoreboard it's inactive child is called Win. I think this because I'm getting this error:

Object reference not set to an instance of an object.

What I'm doing right now that isn't working is assigning values to a scoreboard and win variable at start like so: (before the start method)

GameObject win;
GameObject scoreboard;

In Start():

scoreboard = GameObject.Find("Scoreboard");
win = GameObject.Find("/Scoreboard/Win");

Note: I feel like my problem is assigning win an inactive object, but I could be wrong.

If that's not it I made a method called Reset:

void Reset()
{
    WorldController.IsRunning = false;
    win.SetActive(true);
}

WorldController.cs handles the speed of the world and other things as my game is a runner. Problem isn't here this script has worked fine.

I'm calling the reset method inside of OnCollisionEnter(Collision collision) right here:

 if (collision.collider.tag == "Obstacles")
    {
        hits++;
        lives--;
        L4txt.text = "Lives: " + lives;
        if (hits == 3)
        {
            Reset();
        }

    }

hits and livesare score values(int) that aren't relevant to the question.

Upvotes: 0

Views: 140

Answers (1)

Fattie
Fattie

Reputation: 12272

"Note: I feel like my problem is assigning win an inactive object, but I could be wrong."

EXACTLY!

it won't find inactive obects

SOLUTION!

use a public variable -- drag it in the inspector

Upvotes: 1

Related Questions