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 cregox · Apr 04, 2012 at 09:51 PM · ondestroyondisable

Isolate OnDisable from OnDestroy

I want to check only if the object has been disabled or set inactive, so I can prevent it from being disabled if a certain circumstance is true. I want to ignore when it is being destroyed to prevent errors. Something like this:

 function OnDisable() { 
    if (!beenDestroyed && myStaticVariableIsTrue) { 
       print ("Disabled or Set inactive");
       gameObject.active = true;
    }
 }

Problem is: I don't have a valid `beenDestroyed` and don't know where to get one.

Main reason I want to do this (as I've explained in detail below) is because there are many scripts dealing with enabling and disabling (trough SetActiveRecursively) with objects and only a few of those are to be "protected" in this way - coincidentally some of the top most children of them.

Preamble

Following a similar question, seems like this is how OnDisable currently works, which is almost* (read below) useless:

 function OnDisable() { 
    if (gameObject.active) { 
       print ("Disabled or Destroyed");
    } else { 
       print ("Set inactive");
    } 
 }


Possible Solutions

(A) OnDestroy is only called after OnDisable, so that doesn't help. try and catch simply don't work.

(B) If there's no way to grab a value for beenDestroyed we could remove it, which will bring up 3 lines of error per object:

 (1) !IsDestroying()
     UnityEngine.GameObject:set_active(Boolean)
     TestOnDisable:OnDisable() (at Assets/TestOnDisable.cs:9)
     Assert in file: Assets/TestOnDisable.cs: 9
 (2) !gameObject.IsActive()
     Assert in file: C:/BuildAgent/work/b0bcff80449a48aa/Runtime/Misc/GameObjectUtility.cpp at line: 853
 (3) !m_IsActive
     Assert in file: C:/BuildAgent/work/b0bcff80449a48aa/Runtime/BaseClasses/GameObject.cpp at line: 72

I could ignore the many, many lines due to many objects in scene. Because they don't really matter in this case. The function is pretty much just that. But that would make it very difficult to use/read the Debug Console otherwise and I fear they might actually have some negative effect.

(C) DontDestroyOnLoad probably might be used instead. But it would increase complexity in the code. A lot. I hate using it.

(D) And, of course, I could try and modify so many scripts to prevent disabling these specific objects to begin with... But using it this way would make much more sense programatically while keeping all the scripts much cleaner!

Appeal

Pretty please, any ideas? :-)

Comment
Add comment · Show 3
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 Kryptos · Apr 05, 2012 at 03:12 PM 0
Share

I don't understand what you are trying to do. It is not possible to resurrect an object once it has been destroyed, because this will lead to unstable behaviors.

avatar image Bunny83 · Apr 05, 2012 at 03:25 PM 0
Share

No he want to activate the gameobject when the script get deactivated, but not when it's destroyed.

Anyway, such a setup is just... well... crazy. I can't imagine any setup where the Gameobject is not active and when the script got disabled you need to activate the GameObject ...

If you just want to reactivate the Gameobject when it's beeing deactivated, why have you deactivate it in the first place? You should rethink your event-chain.

avatar image cregox · Apr 05, 2012 at 04:31 PM 0
Share

Thanks for the comments! You both are bringing points I've already discussed in the question, @Bunny83. That tells me it's still not clear enough... I edited it once again. Now I can only urge for people to read it carefully first, hoping I didn't do any more big mistakes there. :)

2 Replies

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

Answer by aldonaletto · Apr 05, 2012 at 05:29 PM

I would try another approach: create your own SetActive function, and avoid disabling those that satisfy some condition - with some special tag, or with a special script attached, etc. You could write a SetActive recursive function like this:

static function SetActive(obj: GameObject, onOff: boolean){
  if (!obj.GetComponent(SpecialScript)){ // only affect objects without SpecialScript
    obj.active = onOff; // set passed object ative/inactive
    for (var trf: Transform in obj.transform){ // repeat for its children
      SetActive(trf.gameObject, onOff);
    }
  }
}
If you want to activate/deactivate the children independently of the parent, just move the for loop outside the if.
Obviously, you can change the condition to whatever you want - this was just an example.
Comment
Add comment · Show 4 · 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 cregox · Apr 05, 2012 at 05:31 PM 0
Share

Thanks so much! You didn't even need to post the code. I still dream to have a solution to the question, but this does solve my issue. ;) - by the way, your web site on your profile is missing a http://

avatar image aldonaletto · Apr 05, 2012 at 08:38 PM 1
Share

Thanks for the warning - ou melhor dizendo, obrigado! Já corrigi o perfil (nem tinha me tocado que o http:// faria falta!)

avatar image cregox · Apr 09, 2012 at 05:29 PM 0
Share

$$anonymous$$ind I ask you, how you apply your knowledge of Unity3D on your pixtar company? I see nothing on the website and, from it, I could never guess you're so good with any kind of program$$anonymous$$g! :P

avatar image aldonaletto · Apr 10, 2012 at 12:45 AM 0
Share

You're right, that site is really a weird choice to be posted in my profile, since it has nothing to do with Unity - but it was the less weird I had (see www.draldo.com.br - this one has even less to do with Unity, for sure).
Pixtar produces electronic horns, an invention of $$anonymous$$e (also known in other countries as "Tarzan horn" or "novelty horn" - see my interview in Jo Soares show: http://youtu.be/iClT1Cgv$$anonymous$$k$$anonymous$$ ).
$$anonymous$$y experience in Unity came from another project started two years ago, which includes game design and control of external hardware, a kind of simulator. We're negotiating this project with a big brazilian company, and hope to replace soon the site reference in my profile to a new one related to this project.

avatar image
0

Answer by playsidepaul · Apr 04, 2017 at 04:37 AM

Found a way around this when the app is quitting, still not sure about scene changing tho.

OnApplicationQuit() is called in the execution order before all the OnDisable()s, so setting a flag in there allows the OnDisable() to have a bit more awareness of if it's being called normally, or from the app shutting down:

     bool quitting = false;
     
     void OnDisable ()
     {
         if (quitting)
             return;
 
         // Do stuff as usual
     }
 
     void OnApplicationQuit ()
     {
         quitting = true;
     }
 



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

7 People are following this question.

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

Related Questions

Does OnDisable()/OnStart() get always called OnApplicationClose? 0 Answers

Detect when GameObject has been deleted / removed from scene 10 Answers

OnDestroy called when object lost focus in editor 2 Answers

OnDisable/OnDestroy Execution Order 2 Answers

OnDisable and OnDestroy Not Get Called - iOS 2 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