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);
	}
}

Don`t use Text as GameObject

  1. Add: using UnityEngine.UI; at the top of your script in order to use the UI library.
  2. Then make a public Text variable and assign your text in the inspector eg: public Text gameOverTxt;
  3. After that, to set your text on and off, access the Texts gameObject by typing: gameOverTxt.gameObject.SetActive (true);

hope that helps!

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.

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;
}

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.