- Home /
SetActive(true) not working
So I made an UIManager to turn my Game Over logo and Play Again text on and off from my GameManager script. When I start the scene it runs TurnOff() method and turns the UI objects off, however when I try to turn them back on nothing happens. I added Debug.log into the TurnOn() method and my console shows the Debug message, however the objects in question aren't being turned back on.
using UnityEngine;
using System.Collections;
public class UIManager : Singleton<UIManager> {
[SerializeField] private GameObject gameOverTxt;
[SerializeField] private GameObject playAgainTxt;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void TurnOn () {
Debug.Log("Turnt on");
gameOverTxt.SetActive(true);
playAgainTxt.SetActive(true);
}
public void TurnOff () {
Debug.Log("Turnt off");
gameOverTxt.SetActive(false);
playAgainTxt.SetActive(false);
}
}
Is it possible that a parent of gameOverTxt and playAgainTxt is inactive? You could use this to check:
public void TurnOn() {
Debug.Log("Turnt on");
gameOverTxt.SetActive(true);
playAgainTxt.SetActive(true);
Debug.Log(gameOverTxt.activeInHierarchy);
Debug.Log(playAgainTxt.activeInHierarchy);
}
See https://docs.unity3d.com/ScriptReference/GameObject-activeInHierarchy.html
I added that Debug.Log line and it shows that it is not active. However, this object is a UI object, that is attached to the canvas (duh), which is attached to the main camera, which is attached to the player, so I'm not sure how any of the parents could be inactive.
Okay, so if I were to Debug this, I would temporarily give the UI$$anonymous$$anager reference to the gameObjects in question, i.e.
[SerializeField]
GameObject player;
[SerializeField]
GameObject camera;
[SerializeField]
GameObject canvas;
and extend the Debug-Output to
Debug.Log(player.activeSelf);
Debug.Log(camera.activeSelf);
Debug.Log(canvas.activeSelf);
to check if any of those are deactivated (maybe by other scripts?).
Of course you could also do a
Debug.Break();
and check those objects in the editor's hierarchy tree manually. $$anonymous$$ight be quicker/simpler.
On a side note, I would find it rather unpractical to have a UI-Canvas attached (indirectly) to the player-object. But that's just personal preference (and I am by no means a very experienced Unity-Developer ;) ) and if it works for you, it's fine of course.
Answer by speechx8 · Feb 20, 2017 at 11:49 PM
Finally made it work! To be honest, I'm not exactly sure what I did to make it work.
I originally had the gameOverTxt and playAgainTxt saved as prefabs, and whenever I entered play mode, they would either disappear from my script, or the actual object in the scene wouldn't turn on. I decided to destroy the prefabs and just use the plain scene objects and use UI.Text instead of GameObjects and what do you know, it started working.
Note: I made a Test Project to mess around with SetActive and was still running into the same problem. It seemed like SetActive wasn't able to keep track of the object after it was deactivated.
Answer by o_doron · Feb 19, 2017 at 10:56 PM
Don`t use Text as GameObject
Add:
using UnityEngine.UI;
at the top of your script in order to use the UI library.Then make a public Text variable and assign your text in the inspector eg:
public Text gameOverTxt;
After that, to set your text on and off, access the Texts gameObject by typing:
gameOverTxt.gameObject.SetActive (true);
hope that helps!
Okay just tried that, however I'm getting the same problem. Whatever game object or text I attach to the script through the inspector doesn't stay attached during play mode.
Answer by speechx8 · Feb 20, 2017 at 10:18 AM
Okay so quick update.
In order for the game object to stay attached to the script during play mode, I had to attach the game object from the prefab instead of from the hierarchy, however running Debug.Log(gameOverTxt.activeInHierarchy); shows it isn't active when SetActive is set to true or false.
Furthermore, I decided to add a reference to the canvas like @jwulf previously mentioned, but since it is not a prefab it leaves my script when entering play mode. But the weird thing is, even though the reference to my canvas leaves the script, Debug.Log(canvas.activeInHierarchy); returns true.
I'm honestly running out of ideas. Any tips or advice would be greatly appreciated.
$$anonymous$$ybe using Resources.Load()
will solve this. or if the above does not help, try using myObject=GameObject.Find(your object name);
and then printing out myObject state.
Answer by Anupam13 · Feb 20, 2017 at 10:22 AM
Instead of enabling and disabling the gameobjects, try doing same for Ui elements - public Text gameOverText;//assign value for text in the inspector public void TurnOn{ gameOverText.enabled=true; }