Search Unity

Start() VS Awake()

Discussion in 'Scripting' started by myunity, Feb 19, 2010.

  1. myunity

    myunity

    Joined:
    Nov 7, 2009
    Posts:
    117
    Hi,

    What is diffrent between this 2 function exactly?

    So thanks.
     
    Necronomicron likes this.
  2. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Posts:
    2,058
    The documentation explains this well.

    http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.Awake.html

    p.s. Wrong forum. Should be in the support forum, not gossip.
     
  3. myunity

    myunity

    Joined:
    Nov 7, 2009
    Posts:
    117
    so thanks.
    but i read the document before.my english is not so good.i did not understand it.if u can help me please.

    so thanks.
     
  4. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Posts:
    2,058
    If you can tell us what your native language is, perhaps there is someone on the forums who could help translate what's in the documentation.
     
    WarHatch likes this.
  5. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Generally the main difference is:

    Awake: Here you setup the component you are on right now (the "this" object)

    Start: Here you setup things that depend on other components.


    if you split it that way you can always be sure that the other object is ready to work with you as game objects in scenes on scene load are loaded in blocks:

    1. All awake are executed
    2. All start are executed
    3. all go into update
     
  6. Vimalakirti

    Vimalakirti

    Joined:
    Oct 12, 2009
    Posts:
    755
    During a game, if you use

    Code (csharp):
    1. gameObject.active = false;
    then later

    Code (csharp):
    1. gameObject.active = true;
    do the object's scripts run their Start or Awake functions again?
     
  7. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Nope, Awake and Start happen once in the lifetime of any game object.
     
    Bunny83, DubWellz, Jordyz96 and 3 others like this.
  8. MadMax

    MadMax

    Joined:
    Aug 5, 2009
    Posts:
    203
    Awake is called first and should be used as the constructor. So you can construct a object with Awake() and create a reference to another script.

    Then start is called. Start is used if you want to delay the order of initialization and pass info to the other script about the constructed object and to do stuff that is not closely tied to instantiation.

    http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.Awake.html

    http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.Start.html
     
    engri, Jason210, luma2057 and 2 others like this.
  9. hitarthdoc1994

    hitarthdoc1994

    Joined:
    Dec 17, 2015
    Posts:
    15
    Thanks for the answer I always got confused in that Matter.
     
  10. cubrman

    cubrman

    Joined:
    Jun 18, 2016
    Posts:
    412
    I would like to add a few of my observations: be aware that some key Unity variables might not be set yet when you call Awake(). For instance: NetBehaviour.isLocalPlayer is always "false" in Awake(), even if it will become "true" inside Start(). Moreover, Awake() is always called before any network messages are received, while you can receive a message before Start() gets called. And the last one: if you set .enabled = false in Start(), the component will still receive messages (like LateUpdate()) during that first frame, while if you do the same in Awake(), no messages will be received.

    Disclamer: all statements above are just my humble observations, don't take them as a rule.
     
  11. Master-Frog

    Master-Frog

    Joined:
    Jun 22, 2015
    Posts:
    2,302
    Wow. Supreme necromancy of this nature should not be taken in stride. We must alert the council, for I fear that He has returned.

    As a novice in programming and writing software in general, I generally like to think of "Awake()" as when my object wakes up in the morning. It's still in bed, so not all of the faculties are there yet. It hasn't put on all of its clothes or brushed its teeth. But it knows who it is. "Start()" I like to think of as basically the first Update(). I don't think of it as being before the the first Update() because I've been burned before.

    In general, Start() is useful for GetComponent() calls and any object that has no Update() logic, and basically just invokes a method or spawns something. Kind of like a "one-off" method. The best part about Start() is that it's like an Awake() that doesn't give you random errors about things not existing yet... and it only happens one time per object. Which, when you think about it, basically lets you write an entire method to avoid creating a bool variable "Initialized" and setting it for true after the first Update() and doing all your setup logic in an if statement... which when you then think about that, it's not much more work to do it that way.

    In the end, Love is really the answer.
     
  12. Billy4184

    Billy4184

    Joined:
    Jul 7, 2014
    Posts:
    6,023
    The way I see it is really simple. Use Awake() for initiating ONLY this component, and Start() for communicating between components before the game starts. That makes sure all components are ready before they communicate, and you have Start() free to do all your world-building and preparation.

    @Master-Frog basically what you said!
     
    luma2057 likes this.
  13. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    ...how so?
     
  14. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    The first "Start" gets called after all of the "Awake"s have been called.

    Awake() gets called during initialisation, where Start() gets called after. So during Awake() you can't assume that other things have finished initialising yet, but by the time Start comes around you can.

    With that in mind, personally, I use "Awake" for any internal setup (ie: all the things I can do without dependency on other GameObjects), and Start() for any external setup (eg: hooking up to other GameObjects). So by the end of Awake() a component should be ready to receive any call from other components, but it can't necessarily make calls to other components because it doesn't know if they've finished setting themselves up yet.
     
  15. Mornedil

    Mornedil

    Joined:
    Feb 7, 2016
    Posts:
    22
    Sounka_01, luma2057 and LeftyRighty like this.
  16. NUTS_ME

    NUTS_ME

    Joined:
    Apr 9, 2017
    Posts:
    1
    So is it like...
    Awake is loading all the game content when i start the game and its kinda all hidden
    Start is all that starting animations(productions loading and stuff)
    Update all that is done in the Main menu after everything is loaded
    ?
     
  17. SUfIaNAHMAD_

    SUfIaNAHMAD_

    Joined:
    Jun 19, 2019
    Posts:
    18
    Really great Community. I am going to be one of you :)
     
    cjjeffery likes this.
  18. egoceyo

    egoceyo

    Joined:
    Jun 12, 2020
    Posts:
    1
    Just sth that I realized if anyone hops on to this thread later on;

    Trying to manipulate the exposed variables of AudioMixer does not work in any Awake() function of a script. I spent at least an hour to try to figure out why my audio did always get reset and reading from PlayerRefs at the start of the game did not affect my audio level until I moved the sliders. I got a bit suspicious of Awake function and tried to set up the exposed variables of AudioMixer in the Start function and it worked without problems.
     
  19. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    Yeah, that's a "known issue" which is "by design" and yet completely undocumented, and has been that way for years. Discussed at length in a thread somewhere.

    It's because some part of the audio system is initialised after Awake(), and that includes setting values from elsewhere which override anything you set in Awake().
     
    Last edited: Oct 22, 2022
  20. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    Link.
     
    Billy4184 likes this.