• 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 ronronmx · Nov 13, 2012 at 05:34 AM · runtimeawakeonenable

Am I using OnEnable() correctly here?

I have read that OnEnable() should really only be used to initialize the GameObject itself, and to add listeners and such, but it shouldn't be used if you need to access other GameObjects or cache references to external components. With that said, Awake() and Start() only get called once, where OnEnable() gets called everytime the GameObject gets instantiated/created/enabled and such.

Here's my code below...I used OnEnable() to self-register t$$anonymous$$s GameObject to a Managers GameObject, so it needs to happen whenever the GameObject becomes available, hence why it's in OnEnable(). Is t$$anonymous$$s a bad use of OnEnable(), and if yes, can someone show me a better way of doing the same t$$anonymous$$ng? Thanks guys!

     public GameObject menuGameObject;
     public GUITexture menuGUITexture;
     
     public static bool isPauseOn = false;
     
     
     void OnEnable( )
     {
         menuGameObject = menuGameObject != null ? menuGameObject : gameObject.FindC$$anonymous$$ld( "menu", true );        // Just in case it wasn't setup already
         menuGUITexture = menuGUITexture != null ? menuGUITexture : menuGameObject.GetComponent<GUITexture>();    // Same as above
         
         Debug.Log("Self registering t$$anonymous$$s GameObject now");
         Managers.HUDManager.SetHUDElement( HUDSettings.HUDType.MENU, menuGameObject, HUDSettings.ShowMenuHUD );
     }
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

1 Reply

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

Answer by Seth-Bergman · Nov 13, 2012 at 06:19 AM

maybe what you read was referring to the fact that OnEnable can't be a coroutine.. Or, that you shouldn't use OnEnable for cac$$anonymous$$ng objects w$$anonymous$$ch only need to be cached once.. For example, move these lines to function Awake():

 menuGameObject = menuGameObject != null ? menuGameObject : gameObject.FindC$$anonymous$$ld( "menu", true );
 menuGUITexture = menuGUITexture != null ? menuGUITexture : menuGameObject.GetComponent<GUITexture>();

once you store these references the first time, why do you need to re-store them the next time you enable the script? Just call t$$anonymous$$s once in Awake, and they are stored, no need to keep re-initializing the same t$$anonymous$$ng...

NOTE: when you create a new instance of an object, the Start and Awake methods DO RUN. That is, in fact, precisely WHEN they run, when they are first created.

So, all you should have in OnEnable is the one line:

 Managers.HUDManager.SetHUDElement( HUDSettings.HUDType.MENU, menuGameObject, HUDSettings.ShowMenuHUD );

t$$anonymous$$s is perfectly acceptable

but you may also need a corresponding line in method "OnDisable", to unregister it..

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 Seth-Bergman · Nov 13, 2012 at 07:04 AM 0
Share

of course, this is only necessary if you are DISABLING the script or object at some point, then re-enabling it.. if you are just trying to add the object when it is first INSTANTIATED, you have no need for OnEnable at all, Awake() would do it all..

avatar image ronronmx · Nov 13, 2012 at 08:19 AM 0
Share

Hey Seth, thanks for the quick answer! I didn't even think about the fact that those 2 lines would get re-cached every-time OnEnable() is called...thx for the tip, makes lots of sense :)

I am not disabling the script, but the GameObject itself, so the reference cached only gets unregistered in OnApplicationQuit(), so I guess you are right, I might not need OnEnable() after all...

avatar image Seth-Bergman · Nov 13, 2012 at 09:14 AM 0
Share

if you are disabling/re-enabling the GameObject (i.e.: gameObject.enabled = false)

then OnEnable MAY make sense.. it runs when BOTH (script AND GameObject) become true/enabled..

the question is, does the item in question NEED to be re-initialized.. should be easy enough to figure out either way through experimentation (if you're not sure), just see if moving the line under investigation into void Start() breaks stuff :)

avatar image ronronmx · Nov 13, 2012 at 11:02 PM 0
Share

Ah got ya, thanks for clearing that up.

After thinking about this a little bit, I figured that no, it doesn't need to be re-initialized every time, just on setup.

Thanks for the help Seth!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Start() not called upon instantation? 1 Answer

Text Mesh not updating during run time? 2 Answers

Adding Prefab Components 1 Answer

Strange Unity state machine behaviour: OnEnable - OnDisable before awake??? 1 Answer


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