- Home /
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.
Answer by Suddoha · Sep 07, 2015 at 06:28 PM
Make sure to disable the player when he dies using
player.SetActive(false);
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.
Answer by nonathaj · Sep 08, 2015 at 02:04 AM
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?
When the user presses the ui button I just want it to reset so the player automatically is set active. @nonathaj
here's the full code:
void OnCollisionEnter2D(Collision2D coll)
{
if(coll.gameObject.tag == "Enemy")
{
player.SetActive(false);
LoseText ();
}
}
void OnGUI()
{
if(player.activeInHierarchy == false)
{
if(GUI.Button(new Rect(10, 10, 50, 50), "Try again?"))
{
Respawn();
}
}
}
Answer by Hsni · Jun 26, 2018 at 08:11 AM
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?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to display an image above 3D object, 0 Answers
UI Escape menu script not working. 2 Answers
if function broken? 3 Answers