• 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
19
Question by mlepage · Apr 08, 2015 at 12:47 PM · scenestartenabledawakedisabled

Something like Awake that runs even if disabled?

The documentation for Start says:

Like the Awake function, Start is called exactly once in the lifetime of the script. However, Awake is called when the script object is initialised, regardless of whether or not the script is enabled. Start may not be called on the same frame as Awake if the script is not enabled at initialisation time. http://docs.unity3d.com/ScriptReference/MonoBehaviour.Start.html

It always seemed to me like Awake would be called regardless of whether an object were enabled or not, whereas Start would be called only when the object became active. And that this would apply to app startup. But that does not appear to be the case.

http://forum.unity3d.com/threads/unityevent-disabled-objects-awake-failure.267241/

My question is whether there is anything like Awake that is called even on disabled objects when the app is started. Because that would be terribly handy. (Or am I misunderstanding Awake, maybe it does somehow run on disabled objects at startup?)

So I think I do understand the difference between Awake and Start. If you Instantiate an object then disable it, Awake will run but Start will not. So you should initialize the object itself in Awake and its connections in Start.

What I'm asking is why can't there be another callback, like Init, which runs only at app startup, for all disabled objects in the scene, if it exists. Unity could make a list of which ones to call when it builds the scene, so it doesn't have to traverse the scene hierarchy at startup. (This is basically how static initialization functions are compiled in some languages.)

At the very least I think the docs should be clearer that Awake is not called at startup if the object is not enabled, because it really seems like it would be, as the docs currently describe.

Comment
Add comment · Show 4
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 Bonfire-Boy · Apr 09, 2015 at 05:00 PM 0
Share

It might help people to try of think of ways to achieve what you're after, if you explain what use you want to put it to.

avatar image puppeteer · Jul 31, 2015 at 06:53 PM 0
Share

Seconded for the question. This is very important if you want for example to check if the mute button should be on or off, and the mute button is part of an options menu.

Now the problem is that you don't want that UI menu to clutter the whole game area while editing, but you can't disable it because it won't make that check on scene startup.

avatar image Bonfire-Boy · Aug 01, 2015 at 07:45 PM 0
Share

I agree that the documentation isn't too clear, but the way things work seems pretty good to me. If I put an object in my scene in the editor but deactivate it, I want it to be like it's not even there... until I activate it.

But I think there are ways of achieving what you're after. For example, you can have another object in the scene, with a reference to the deactivated object(s), which calls their initialisation function(s) in it's Awake/Start. That would probably be my approach to puppeteer's situation - I'd initialise bits of my UI from a UI$$anonymous$$anager of some sort, so that they could be initialised regardless of whether they're active.

Another approach would be to make the object active but have its components disabled, so that their Awakes get called on scene start up but that's all, until they're enabled.

But I should say that neither of the above is something I've ever actually found myself needing to do, so it may be that I'm missing something.

avatar image tormentoarmagedoom · Nov 05, 2018 at 06:50 PM 0
Share

Objects are active or inactive. The components like a script, are enabled or disabled

11 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by Lumpazy · May 04, 2019 at 04:43 PM

Oh yeah Earth to unity : Anyone there who would think of mentioning, that a disabled scripts Awake function is executed in the actual documentation of Awake ?

Oh no. It makes Soooo much mooore seeense to mention it in the documentation for 'Start'

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
0

Answer by Xtro · Jan 10, 2017 at 07:50 PM

I released this asset for free quite some time before you asked this question.

"We Need Created Message"

https://www.assetstore.unity3d.com/en/#!/content/23453

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
0

Answer by Schnodahipfe · Jul 24, 2017 at 12:15 PM

Another option, which I use is the following:

  1. Have the gameobject active in the hierarchy

  2. Do all initializations in Awake()

  3. Then deactivate the gameobject in Awake() after the initializations

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
0

Answer by ModLunar · Mar 24, 2019 at 05:19 PM

This might not help your scenario, but I was running into similar problems with wanting Awake to be called on deactivated GameObjects and children.

My situation was: I needed to deactivate my HUD GameObject at the very beginning of the game (because my game starts at the title screen, where the HUD should not be shown). But this caused Awake() to not be called on all the children of the HUD GameObject!

In Awake() in my HUD script (attached to the root GameObject of the HUD), I was using: gameObject.SetActive(false);

And consequently, all of the children scripts were not having their Awake()s being called at all. However, I found out that one possible way to help this is to deactivate the GameObject on Start() instead. Start() is still called early enough that, if I deactivate the object then, we won't see the HUD flash on the screen at all for a frame -- but it happens after all the Awake()s are called!

 using UnityEngine;
 
 public class HUD : MonoBehaviour {
     public void Start() {
         gameObject.SetActive(false);
     }
 }



I hope this helps! :)

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
0

Answer by vfxjex · Sep 30, 2020 at 10:11 PM

Just in case someone bumps into this problem 1st - I made a gameobject called Initializer with this simple script attached, using UnityEngine;

 public class Initializer : MonoBehaviour
 {
     public delegate void Initialize();
     public static Initialize initialize;
 
     void Awake()
     {
         initialize();
     }
 }

2nd - in your script that you want to initialize, add a Constructor and Initialize Method like this,

       using UnityEngine;
         public class MyScript: MonoBehaviour
         {
         bool isInitialized;
             MyScript()
             {
                 Initializer.initialize += Initialize;
             }
         
             void Initialize()
             {
                 if(this == null)
                 {
                     Initializer.initialize -= Initialize;
                     return; 
                 }
                 if (isInitialized)
                 {
                    Initializer.initialize -= Initialize;
                    return;
                 }
                 isInitialized = true;
                 Debug.Log("I WAS INITILIZE");
             }
         }
     


Comment
Add comment · Show 2 · 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 Bunny83 · Sep 30, 2020 at 10:47 PM 0
Share

While this does solve any issues with the loading thread (since you delegate the initialization to another Awake method) this comes with several other issues. First of all in the Unity editor the objects might be created / recreated more often than you may think. So this could still be an issue. Likewise you introduce a memory leak because since you register an instance method to that delegate, that delegate will keep those objects alive forever. This could result in all sorts of hidden issues. At least clear the initialize delegate after you called it.


Similarly if this script ends up on a prefab, since your Initializer awake method is only called once any object instantiation after the Awake method has been called will still register the delegate but it will never be called or cleaned up.

avatar image Xtro · Sep 30, 2020 at 11:00 PM 0
Share

Doesn't work when the gameobject is disabled.

  • ‹
  • 1
  • 2
  • 3
  • ›

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

38 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make Awake and Start get called in order with additive scenes? 1 Answer

does finction start or awake run when the object or script is enabled mid game 3 Answers

Start/Awake cut off at a certain point? 1 Answer

"Main" function? 3 Answers

Settings, Game icon, Start Unity scene... 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