Should I be using activeInHierachy or activeself? - c#

Hi, when the player dies in my game I want a ui button to appear on the screen but when I hit play and the player dies no ui button appears. At first I used: if(player.activeSelf == false) however it didn’t work so I tried if(player.activeInHierarchy == false)this also didn’t work.

What I want know is if .activeSelf is what I’m supposed to be using or .activeInHierarachy(if so how should I used them) or something different all together.

Make sure to disable the player when he dies using


What you want to use depends on your needs.
activeSelf returns the own state in disregard of any parents states. activeInHierarchy can return false even though the object you’re referring to is active, because any inactive parent to the current object will make this property return false.

GameObject.activeInHierarchy represents if the object is active within the entirety of the scene. An object can be made inactive if their gameobject or any of their parent’s gameobjects have been set to inactive.

GameObject.activeSelf represents if the gameobject has been set to inactive. This ignores any parent’s active state, and simply returns if our individual gameobject’s state is active or not.

It sounds like you want to use activeInHierarchy, as you want to check if the object is active within the scene. Also, you never mention calling GameObject.SetActive on your UI button, are you sure you are setting it to active?

activeSelf: Is the checkbox on top left of the inspector enabled?
activeInHierarchy: Is the gameObject + it’s parents all the way upto to are active?