gameObject.SetActive (true); Not working

So I am making a game over UI and setting the game object that it’s in at to inactive when I start the game but I can’t get it to reactivate.

public class gameManager : MonoBehaviour {

	public GameObject gameOverUI;

    void Start () {

		Debug.Log ("turn off ui");
		gameOverUI.SetActive (false);

	}

public void gameOver(){

		Debug.Log("GameOver");
		gameOverUI.SetActive (true);
	}

This is a condensed version of my gameManager code, both of the Debugs are printing and when I have the gameObject on it disappears when I start the game so it must be linked correctly. I just can’t get it to show again. Anyone know why?

The “gameOver” function must be a callback from an active object.
So… Is the object that calls this function activated?

For instance, if the “gameOver” callback is running from an Update function from an INACTIVE object, it won’t be called.

Hi! I had same problem, but i found why this is happens.

  1. You made GameObject in design time.
  2. Made it inactive.
  3. Start play
  4. GameObject is still inactive.
  5. You call gameOver()
  6. GameObject activates first time
  7. Unity calls Start() method (because script activate first time.). GameObject deactivated again.

So! You have two variants:

  1. Makes GameObject initially as inactive (in Design Time) and remove deactivation from Start() method.
  2. Makes GameObject initially as active (in Design Time) and keep deactivation in Start() method.

Good luck.

Your code seems to be perfectly ok. The only posible problem i can imagine of is a hirerchy problem. If you have a parent/grandparent GameObject disabled, this fact will have prority and the GameObjectt you want to activate wont. Perhaphs this can help you.

Otherwise check if your code depends on that other object. But i dont think it’s the case scince you are using a game manager.

Hello , Did you check the call of your function gameOver ?
Or maybe you setActive(false) the gameOverUI after the function or in others scripts ?

I might be wrong, but surely if you have a script on an inactive gameObject, that script won’t be running?

From the looks of your code, the GameOverUI is a variable, so if you had a manager object separate from the UI object then it would run fine, however you say in the question “the game object that it’s in”, so thought it was worth checking.

If it was the latter, make a new gameObject that is always active, and move the script onto that. It can be a “manager” object that handles activating UI and so forth. Just a heads up as well, if you want to access the gameObject that a script is attached to, “gameObject” can be used instead of making a variable to hold itself. :slight_smile:

Make sure this script is not attached to the disabled object.

why not just put the gameOverUI to be inactive at the hierarchy… you won’t need that void Start() anymore.

Make sure you dragged and dropped from the hierarchy instead of the prefab onto your component.

1 Like

I have the same problem. My GameOverUI Panel won’t activate when I call the GameOver method, although the method is running (I checked with Debug.Log). I tried another UI Panel on this same method and it did not open either.

I tried opening the GameOverUI Panel on my Victory method and this time it worked.

I have reread my code a thousand times, but I cannot see where the mistake is. The most annoying thing about all this is that the GameOverUI was one of the first things I created in the project and it was working just fine in the beginning.

public void GameOver()
{
    Cursor.visible = true;
    gameOverPanel.SetActive(true);
    Debug.Log("Game Over.");
    Time.timeScale = 0;
}

public void Victory()
{
    Cursor.visible = true;
    victoryPanel.SetActive(true);
    Time.timeScale = 0;
}

i may be wrong but u have to use some kind id of “” if ""statement to make it work
like in my
if (gameover = anything)

{
gameOverUI.SetActive (true)
};

then its going to work fine