Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by darbotron · Oct 20, 2013 at 08:25 PM · gameobjectparentsetactiveselfondisable

Is it possible to determine if OnDisable() is being called from parent.SetActive( false ) or self?

I have a class derived from monobehaviour.

For various reasons when OnDisable() is called I need to know whether it was triggered by:

a) the GameObject it is a component of having SetActive( false ) called on it, or...

b) ...whether SetActive( false ) was called on a GameObject higher up the hierarchy

is it possible to tell, and how can you do it?

**N.B. I couldn't find any questions that asked this, and since I have now solved this problem I thought I would share the information with the community by asking the question and answering it myself.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by darbotron · Oct 20, 2013 at 08:46 PM

There are two properties of GameObject that give information about its active state:

  • GameObject.activeSelf - this flag is only set by use of SetActive() on the GameObject it is a member variable of

  • GameObject.activeInHierarchy - this flag is also set by SetActive() on the GameObject, but also by use of SetActive() on any parent GameObject

See the GameObject documentation: http://docs.unity3d.com/Documentation/ScriptReference/GameObject.html

also see the 'GameObject active state' for more detail on how these flags work: http://docs.unity3d.com/Documentation/Manual/UpgradeGuide3540.html

Based on tests I did, it seems these flags are set by the Unity code before OnDisable() is called.

Given this information you can use these flags to determine which case a), or b) is true in the question:

a) both flags will be false in this case (SetActive( false ) was called on this object)

b) activeInHierarchy will be false, and activeSelf will be true (SetActive( false ) was called on an object upstream in the hierarchy)

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
1

Answer by Hoeloe · Oct 20, 2013 at 08:32 PM

The only way you could do this is by wrapping up the function SetActive within the class itself. There, you could set a flag in the wrapper, which tells the program "I am being called from within this object", that you would reset at the end of the OnDisable method. That way, if the flag hasn't been set, then it is being called from elsewhere, while if it is set, then it is being called from within the current script.

Comment
Add comment · Show 7 · 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 darbotron · Oct 20, 2013 at 08:55 PM 0
Share

This would do it too, but in my use case I can just use the Unity flags :)

avatar image Hoeloe · Oct 20, 2013 at 09:05 PM 0
Share

Ah I see, I wasn't sure whether those would work for your case, but if they do, then that's great.

avatar image darbotron · Oct 20, 2013 at 09:24 PM 0
Share

Your way is more bulletproof and generic, have chosen it as the best answer :)

avatar image Hoeloe · Oct 20, 2013 at 09:42 PM 0
Share

Your tests will probably prove to work well, my method is more general use, and is guaranteed to work as expected (using Unity's flags may produce problems if they do odd things, as you can't see the source code), but may be less readable or efficient than using Unity's flags, depending on how you use them.

avatar image darbotron · Oct 20, 2013 at 10:07 PM 1
Share

LOL! after more thought - I've re-voted my (edited!) answer as the correct one...

Here's why:

There are only 3 valid states of these flags (let's call activeSelf "S", activeInHierarchy "H")

  1. S: true H: true

  2. S: true H: false

  3. S: false H: false

  4. S: false H: true - NOT VALID

OnDisable can only be called by 3 transitions:

  • state 1 to 2 if SetActive( false ) called on upstream object

  • state 1 to 3 if SetActive( false ) called on self

  • state 2 to 3 if SetActive( false ) called on self

which matches my original answer - thanks for the help dude, interesting hour or so of discussing this ;)

Show more comments

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

17 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

Related Questions

Can a parent SetActive a child GameObject anymore 2 Answers

How can I get my own gameobject? 2 Answers

Can't activate game object. 3 Answers

Zombie won't die 2 Answers

Calling setActive(false) on a disabled object? 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