Search code examples
c#unityscriptunity-game-engineunity3d-2dtools

Enable object Parent in the hierarchy


How do I get an object in the hierarchy is enabled through scrip?

void OnCollisionEnter2D (Collision2D colisor)
    {
        if (colisor.gameObject.tag == "Floor") {

            Destroy (gameObject, 0.6f);

            } else {

            if (colisor.gameObject.tag == "Bee") {

                coinCollectSound.Play ();

                heart = GameObject.FindGameObjectWithTag ("Hearts").GetComponent<Hearts> () as Hearts;

                if (heart.TakeHeart ()) {
                    Destroy (gameObject);

                } else {

                    //Here i want setActive(true) Object parent in hierarchy  called "GameOver"             
                    //And setActive(false) Object in hierarchy Called "Game"

                }
            }
        }
    }

I do not want to call a different scene for the Game Over, but now I just want to enable it.


Solution

  • You should access the GameOver using GameObject.Find("GameOver"), but if it's disabled you won't be able to do that. Instead, create a public GameObject variable on a enabled script that references the object you wanna SetActive. Then, find the object by type and access it's variable, enabling it.