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?
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.
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.
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.
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.
Are you sure gameOver() method is being called? Or that its not another part of your script that is modifying this behaviour?
– ShadyProductionsI 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.
– pr_0_0