enabled = true doesn't work the first time!

I have a script that I swore worked fine in 4.3 but performs strangely in 4.5:

The debug output from using this script in my project is “enabled”, True, “disabled”, “enabled”, True, “showing”, “showing”, “showing”… etc…

In other words, the first time it triggers, it refuses to show the GUI text. Only the second time it gets triggered does the text show up:

using UnityEngine;
using System.Collections;

public class TileEventMessage : TileEvent {

	public Font font;

	public string message;

	private bool permanent;
	private bool messageShown;

	public override void Start () {
	
		// Temp stuff that should be overwritten
		//permanent = false;
		//message = "DEFAULT MESSAGE";

		messageShown = false;

		enabled = false;
	}
	
	public override void OnTriggerEnter (Collider other) {

		if (!permanent && messageShown) {
			return;
		}
		Debug.Log ("enabling");
		enabled = true;
		Debug.Log (enabled);
	}

	public override void OnTriggerExit (Collider other) {

		if (!permanent) {
			messageShown = true;
		}
		Debug.Log ("disabling");
		enabled = false;
	}

	void OnGUI () {
		Debug.Log ("showing");
		GUI.skin.font = font;
		GUILayout.Label(message);
	}
	
	void SetMessage (string m) {

		message = m;
	}

	void SetPermanence (bool p) {

		permanent = p;
	}
}

sorry to revive an old thread, but this was the first result when I googled the same issue. For anyone else looking for a solution for this, this kind of thing seems to happen because there’s some kind of setup happening at scene start that allows it to actually display, so starting with the gameobject containing the GUI stuff disabled will keep it hidden the first time you enable it.

As workarounds, either

  1. Only enable/disable the actual component instead of the gameobject. In this case: GetComponent<Text>().enabled = true; instead of enabled = true (deprecated now anyway) or SetActive(true); or

  2. Start the scene with the gameobject enabled and disable it on the first LateUpdate so it can get itself set up first with something like if (firstRun) {SetActive(false); firstRun = true;}. I ended up using this way because I’m working with a parent object that has a bunch of children with UI elements that I want to turn on/off in one go.

9 years on and this is still an issue…