Checks whether a component is enabled, attached to a GameObject that is active in the hierarchy, and the component's OnEnable has been called.
Behaviour.isActiveAndEnabled
returns true
only if all the following conditions are met:
true
).true
).
Important: Even if a component is enabled and its GameObject is active, isActiveAndEnabled
still returns false
until OnEnable
is called on the component. This is by design in Unity's scripting lifecyle. For more information, refer to Event function execution order in the User Manual.
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class Example : MonoBehaviour { public Image pauseMenu;
public void Update() { //Checks if the GameObject and Image are active and enabled. if (pauseMenu.isActiveAndEnabled) { //If the Image is enabled, print "Enabled" in the console. Stops when the image or GameObject is disabled. Debug.Log("Enabled"); } } }