How to tell if something is enabled

I need to tell if something is activated. In my script, I need to tell whether a gameobject is activated or not. Such as, if this gameobject is activated, then do this. This is in c#

2 Answers

2

gameObject.activeSelf returns bool , so we can test its status in if condition.

Code for that like this,

if (gameObject.activeSelf )
   print("It is Activated.")

if you want to check in everyframe, it should be in Update() method.

Use the property enabled, this is derived from the Behaviour class that MonoBehavior derives/inherits from.

Though keep in mind that if enabled is false, the update method for that class is not called. You may want to implement OnEnable, when something is enabled this is called, then the Update method is called each from on that component.

You may want to observe whether the GameObject is active however, the documentation has code:

GameObject.SetActive

GameObject.activeInHierarchy

GameObject.activeSelf