Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by animeman0 · Sep 07, 2015 at 05:12 PM · c#uiif

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
8

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
3

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?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image animeman0 · Sep 08, 2015 at 04:49 PM 0
Share

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();
             }
         }
     }

avatar image
0

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

32 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges