- Home /
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?
Are you sure gameOver() method is being called? Or that its not another part of your script that is modifying this behaviour?
I have a similar problem. In one scene my this code is working absolutely perfectly but in the next scene it is not working. The hypocrisy.
Answer by MeronSoda · Mar 15, 2017 at 03:35 PM
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.
Answer by body · Mar 16, 2017 at 08:29 PM
Hi! I had same problem, but i found why this is happens.
You made GameObject in design time.
Made it inactive.
Start play
GameObject is still inactive.
You call gameOver()
GameObject activates first time
Unity calls Start() method (because script activate first time.). GameObject deactivated again.
So! You have two variants:
Makes GameObject initially as inactive (in Design Time) and remove deactivation from Start() method.
Makes GameObject initially as active (in Design Time) and keep deactivation in Start() method.
Good luck.
#6 won't happen (and so #7 won't either).
An inactive object doesn't benefit from lifecycle events. You can call functions on inactive objects (doing so will not activate an object), but if you rely on lifecycle events to initialize anything used by your function, you'll find null reference issues.
Answer by Matt1000 · Mar 16, 2017 at 04:25 PM
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.
Answer by KrazyApple · Mar 15, 2017 at 02:21 PM
Hello , Did you check the call of your function gameOver ? Or maybe you setActive(false) the gameOverUI after the function or in others scripts ?
Answer by joshua-lyness · Mar 15, 2017 at 02:28 PM
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. :)