Am I using OnEnable() correctly here?

I have read that OnEnable() should really only be used to initialize the GameObject itself, and to add listeners and such, but it shouldn’t be used if you need to access other GameObjects or cache references to external components. With that said, Awake() and Start() only get called once, where OnEnable() gets called everytime the GameObject gets instantiated/created/enabled and such.

Here’s my code below…I used OnEnable() to self-register this GameObject to a Managers GameObject, so it needs to happen whenever the GameObject becomes available, hence why it’s in OnEnable(). Is this a bad use of OnEnable(), and if yes, can someone show me a better way of doing the same thing? Thanks guys!

    public GameObject menuGameObject;
	public GUITexture menuGUITexture;
	
	public static bool isPauseOn = false;
	
	
	void OnEnable( )
	{
		menuGameObject = menuGameObject != null ? menuGameObject : gameObject.FindChild( "menu", true );		// Just in case it wasn't setup already
		menuGUITexture = menuGUITexture != null ? menuGUITexture : menuGameObject.GetComponent<GUITexture>();	// Same as above
		
		Debug.Log("Self registering this GameObject now");
		Managers.HUDManager.SetHUDElement( HUDSettings.HUDType.MENU, menuGameObject, HUDSettings.ShowMenuHUD );
	}

maybe what you read was referring to the fact that OnEnable can’t be a coroutine… Or, that you shouldn’t use OnEnable for caching objects which only need to be cached once… For example, move these lines to function Awake():

menuGameObject = menuGameObject != null ? menuGameObject : gameObject.FindChild( "menu", true );
menuGUITexture = menuGUITexture != null ? menuGUITexture : menuGameObject.GetComponent<GUITexture>();

once you store these references the first time, why do you need to re-store them the next time you enable the script? Just call this once in Awake, and they are stored, no need to keep re-initializing the same thing…

NOTE: when you create a new instance of an object, the Start and Awake methods DO RUN. That is, in fact, precisely WHEN they run, when they are first created.

So, all you should have in OnEnable is the one line:

Managers.HUDManager.SetHUDElement( HUDSettings.HUDType.MENU, menuGameObject, HUDSettings.ShowMenuHUD );

this is perfectly acceptable

but you may also need a corresponding line in method “OnDisable”, to unregister it…