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
11
Question by darthbator · Jan 18, 2013 at 12:31 AM · assetawakebestpractices

Use of Awake() vs Start()

I as wondering if there was a general best practices use for using Start() versus using Awake()? Pretty simple question, but I didn't find a good answer that seemed canonical in my googleing.

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

4 Replies

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

Answer by Loius · Jan 18, 2013 at 12:42 AM

All Awakes run before any Starts run before any Updates. You usually use Awake if you need something initialized so somebody's Start function can use it.

Comment
Add comment · Show 6 · 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 aldonaletto · Jan 18, 2013 at 12:50 AM 1
Share

Another hint: never use Find functions in Awake, only in Start - Awake runs when the object is instantiated, and the target object may not have been created yet.

avatar image bdjnk · May 14, 2013 at 06:45 AM 6
Share

"Awake is called after all objects are initialized so you can safely speak to other objects or query them using eg. GameObject.FindWithTag." - $$anonymous$$onoBehaviour.Awake

"The difference between Awake and Start is that Start is only called if the script instance is enabled. This allows you to delay any initialization code, until it is really needed." - $$anonymous$$onoBehaviour.Start

avatar image whydoidoit · May 14, 2013 at 06:53 AM 0
Share

You can certainly Find and GetComponent another script in Awake, however you have no knowledge as to whether that script has executed it's Awake yet and hence whether it is in any state to receive actual messages or requests. So message passing should be deferred until Start.

avatar image Bunny83 · May 14, 2013 at 07:47 AM 2
Share

And that's only true for scene-serialized objects when a level is loaded. When you use Instantiate Awake is even called before the Instantiate call returns. Start is always delayed to the start of the next frame. If you setup links in Awake you can't adjust something when you instantiate a prefab. Start is the better place in most cases.

Like someone else said: use Awake to initialize the object itself, use Start to setup links to other objects.

avatar image Bunny83 · May 14, 2013 at 07:51 AM 0
Share

[$$anonymous$$eta]

I'm just wondering. We had this question a couple of times, but looking at the related questions just brings up ??? in my $$anonymous$$d. How are those related questions? This looks like random picking... There are things about shaders, item pickup, draw calls, $$anonymous$$mwork, memory leaks, static variables, ... but nothing actually related to the question.

Show more comments
avatar image
15

Answer by greggman · Jun 24, 2015 at 11:44 AM

The accepted answer is wrong :( The correct answer is in the comments from bdjnk

"Awake is called after all objects are initialized so you can safely speak to other objects or query them using eg. GameObject.FindWithTag." - MonoBehaviour.Awake

"The difference between Awake and Start is that Start is only called if the script instance is enabled. This allows you to delay any initialization code, until it is really needed." - MonoBehaviour.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
3

Answer by GregoryFenn · Jun 11, 2018 at 11:38 AM

You might like to copy this beefed-up template of a standard MonoBehaviour class:

   using System.Collections;
   using System.Collections.Generic;
   using UnityEngine;

   public class MyBestClassEver : MonoBehaviour { 


   /* MAIN DIFFERENCES BETWEEN Start & Awake:
     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:
 
       * All Awake() methods are executed, even if the object's component is disabled, then
       * all Start() methods (if the relevant component is enabled) are executed; then finally
       * all enabled objects go into Update().
     
 */


 
     // Awake is called when the script is being loaded, even if the component is not enabled.
     // Awake is used to initialize any variables or game state before the game 
     // starts. Awake is called only once during the lifetime of the script instance, 
     // after all objects are initialized, so you can safely speak to other objects 
     // or query them using, eg, GameObject.FindWithTag. Each GameObject's Awake 
     // is called in a random order between objects. Because of this, you should 
     // use Awake to set up references between scripts, and use Start to pass any 
     // information back and forth. 
     // *Start is always called after any Awake functions*, this allows you to order 
     // initialization of scripts. Awake cannot act as a coroutine.
     // For C# use Awake instead of the constructor for initialization, as the 
     // serialized state of the component is undefined at construction time. 
     // Awake is called once, just like the constructor.
     void Awake () {
         print("Object Awake!");
 
     }
 




 
 
     // Use this for initialization and finding objects
     // The difference between Awake and Start is that Start is only called if 
     // the script instance (component) is enabled. This allows you to delay any 
     // initialization code until it is really needed.
     // Also all Awakes run *before* any Starts.
     // Start will only be called on an (enabled) object before its first Update.
     // By delaying or controllying when a component is enabled, you can modify the
     // initialisation process of gameObjects and variables.
     // Note that both Start and Awake are called at most once, eg Start would not
     // be rerun by disabling and re-enabling.
     void Start () {
         print("Object Start");
         
     }
 
 
 
 
 
 
 
 
 
     
     // Update is called once per frame at a variable rate, with gaps of Time.deltaTime .
     void Update () {
         print("Frame update");
     }
 
 
 
 
 
     // FixedUpdate is called at a constant rate, independent of the frame-rendering rate.
     // The (constant) gap between calls to FixedUpdate is Time.fixedDeltaTime .
     // This makes is ideal for motion, collision and other physics work, since these events
     // should be processed regularly and in-sync regardless of how fast or slow your 
     // computer is rendering the visual game frames or user-interface.
     // In order to get the elapsed time since last call to Update(), use Time.deltaTime.
     // You can speed-up or slow-down all physics calculations in the world by changing 
     // the value of Time.timeScale (which is set to 1.0f by default, obviously). E.G., if
     // Time.timeScale is set to zero, the game is basically paused (if all your functions
     // are framerate-independent).
     void FixedUpdate () {
         print("Fixed-rate update call");
     }

 }




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
1

Answer by J.Mad · May 31, 2015 at 11:25 AM

From my little experience: Awake is something like a constructor. Start is something like a "do this once" as the first Update. Update is update.

If I instantiate a GameObject and just after that I want to call its public method I have to initialize all necessary fields in Awake of that GameObject, not in 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

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

17 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

Related Questions

i need a simple dynamic AI system ? 0 Answers

Can a big resources.assets file cause issues on iOS ? 4 Answers

Script to load a text file gives error. 1 Answer

Best tools for asset making? 0 Answers

Create GameObjects/Components by Dragging Assets to Hierarchy/Inspector 5 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