Search Unity

What is the difference between void start() and void awake()?

Discussion in 'Scripting' started by Unity3Dkannan, Jun 12, 2012.

  1. Unity3Dkannan

    Unity3Dkannan

    Joined:
    May 19, 2012
    Posts:
    43
    WHAT IS THE DIFFERENCE BETWEEN VOID START() AND VOID AWAKE()?


    I HAVE SAMPLE SCRIPT PLEASE EXPLAIN THIS
    Code (csharp):
    1.  
    2. private int out;
    3.  
    4. void Start()
    5.  
    6. {
    7.  
    8. out=0;
    9.  
    10. }
    11.  
    12.  
    13. void awake()
    14.  
    15. {
    16. out=1;
    17. }
    18.  
    19. debug.log("what is the difference?="+out);

    :rolleyes:
     
  2. TylerPerry

    TylerPerry

    Joined:
    May 29, 2011
    Posts:
    5,577
    Awake happens first.
     
  3. kor

    kor

    Joined:
    Oct 29, 2009
    Posts:
    122
  4. BigBoy

    BigBoy

    Joined:
    Jan 5, 2012
    Posts:
    91
    start call when the program start but awake call when you initialize the object into that.
     
    Miaw666 likes this.
  5. TylerPerry

    TylerPerry

    Joined:
    May 29, 2011
    Posts:
    5,577
    That is wrong.

    The docs clearly say "The difference between Awake and Start is that Start is only called if the script instance is enabled"
     
    unity_Y6mc6aP603oa2A likes this.
  6. BigBoy

    BigBoy

    Joined:
    Jan 5, 2012
    Posts:
    91
    ya difference between Awake and Start is that Start is only called if the script instance is enabled, i never refuse this. but here i say if both the function have same instance which is first execute.
     
  7. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    Awake is invoked when the MonoBehaviour is created. You may view it as your default constructor.

    Start is a message which is sent to MonoBehaviours when all initialisation is done and the first frame for the behaviour is about to run. As mentioned, disabled behaviours do not get the start message as they are indeed not about to run their first frame.

    So the Start message is sent just before the first Update call on the same behaviour. At this point most dependencies should be fully initialised.

    A key difference between a function invoke which is used for Awake and a message transmission which is used for Start is that the handler for the latter can be turned into a co-routine.
     
    SgerbwdGwyn likes this.
  8. KnightRiderGuy

    KnightRiderGuy

    Joined:
    Nov 23, 2014
    Posts:
    515
    So if you have a game object with a script attached and in the inspector it's disabled, and when you enable that game object the Start function in the script is then activated? Or both Start & awake? been trying to understand that fully for a while now lol ;)
     
  9. kaskiU

    kaskiU

    Joined:
    Aug 6, 2013
    Posts:
    50
    kristinhero and naviln like this.
  10. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Awake gets called immediately the first time a GameObject is enabled. This is when it's Instantiated for enabled GameObjects, or when ts enabled for disabled ones.

    Start gets called immediately before the first frame that a GameObject is enabled for.

    The beauty of this system is Awake will be called for all GameObjevts before Start is called. Which is why the two methods exist. Use Awake to set up a Component internally, and Start to set it up externally.
     
    scharlott and naviln like this.
  11. Crispried

    Crispried

    Joined:
    May 23, 2016
    Posts:
    24
    In this case both. If your script is disabled it's first of all mono behaviour wasn't created, so Awake function won't invoked. And also if your script is disabled it can't recieve messages, so you message functions like Start, Update, FixedUpdate and LateUpdate, etc also won't work.
     
  12. Honorsoft

    Honorsoft

    Joined:
    Oct 31, 2016
    Posts:
    81
    So, for anyone still confused...

    -On a script that is disabled: Nothing is called (No Awake or Start functions called).
    -On a enabled script: Awake is called first when an object 'carrying' the script is first activated(enabled/initialized), Start is then called when the game/application proceeds.

    This might be of interest to those who need to perform tasks before an object is actually 'instantiated' into the game.
     
    Last edited: Jan 7, 2018
    Eralven, GHOST8476 and Kiwasi like this.
  13. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    Thank you, oh righteous necromancer, saviour of us noobs!

    Pro tip:
    Forum email notifications are not subject to the edit of your original "why can't noobs resist posting wrong answers? It just makes right answers harder to find" wording.​
     
    Kotmannweilnice and Dudechester like this.