Hi all,
I’m a new Unity3d user and I would like to know why we should use
Awake, Start, Update, fixedUpdate and other functions.
I know Awake is used before Start, but reading the scripting documentation it makes me think that Awake and Start are the exact thing is it optional to use awake or I’m forced to use it?
reading the documentation makes it a lil bit difficult to understand
I would like to know with Examples by you guys.
I use Csharp language btw
Awake is called when the object is created/loaded. You use it to make some internal initialisation to the gameObject (create other components, generate a mesh, …)
Start is called on the first frame the script becomes active, which generally happen when the game starts but it can be later on as well.
I know you’ve mentioned the documentation is difficult to understand, but the following page is a good reference to come back to from time to time to check when these functions are called exactly:
It does make a difference.
Not long ago I was stuck with an error that couldn’t figured out how to fix.
After some debugging I manage to fix the bug by simply change void Start() to void Awake().
I was running a method on void Start() that load a list with objects that will be used in other scripts, The problem was that the other scripts that needed this list ran first before I was even loading items into it I kept getting null error.
ericbegue already gave you a perfect explanation of when it’s called.
If what you are asking is what you should code inside it, I would say everything you want to initialize call in Awake() and everything that you want to happen as soon as the game start call in Start()
Another way of deciding what goes where: Any initialization that does not depend on any other game object or component goes into Awake(). Any initialization that does depend on one or more other game objects or components goes into Start(). That way, all of the initialization in Start() can be guaranteed that the other objects/components being used have already been at least minimally initialized in Awake(), at least to the extent that they could be without referencing other objects/components.
As for the update functions, I think of FixedUpdate() as being used for all constantly executed/checked code related to game mechanics, and Update() for code that either isn’t consantly being executed or isn’t related to core mechanics (stuff like presentation, e.g., rendering, audio). LateUpdate() is mostly just a convenience for when some presentation logic is dependent on other presentation logic which executes earlier each frame, and I don’t want to mess with script execution order numbers.
In general, I’ve found that it’s best to naively put everything in Start() or Update() and only move things from Start() into Awake() or OnEnable(), or from Update() to FixedUpdate() or LateUpdate() after I’ve developed a problem and better understand the problem space, the needs I have, and the usage benefits of the different event functions available. That’s worked out to be an effective learning style for me, rather than trying to have pre-baked principles which I don’t fully understand handed to me from elsewhere.
It doesn’t call awake then start on one object before moving to the next. Instead awake is called on all objects, then after that, start is called on all objects.
No, it’s only ever there for Physics and Physics2D. That’s the sole purpose for FixedUpdate by design.
Why do you say that? As I understand it, the design of FixedUpdate is simply to execute at a reliable frequency, independent of the render frequency. This is indeed something that physics engines generally need. But it’s also often what I want my game mechanics to do, whether or not they are physics-related. I like the determinism that it can bring, and dislike depending on Time.deltaTime for anything that isn’t merely presentation.
Granted, it forces all fixed updates application-wide to share the same frequency with the physics engine, so if I need them to be separated, I’ll have to roll my own solution instead of using FixedUpdate. But that generally isn’t necessary.
Because it’s not a reliable frequency at all. Sometimes it will fire back-to-back, twice or more in a row, sometimes it will not execute when you expect it to. Because it’s not independent and it’s not threaded: The truth about FixedUpdate() - Unity Engine - Unity Discussions
Basically you could just use a repeating co-routine for identical results, or a timer in Update.
guys so in Start() for example I can put HUD/GUI is that correct?
since in MONODEVELOP the Start() is used for the initialization, I mean before everything start in the “game”
sorry guys, but my main language is not English and in Italian guide is not available. And I try my best to understand sorry if I keep asking and try to comment my Understading.
When Awake is called after a scene is loaded the following things have happened.
All GameObjects have been created
All Components have been added to their GameObjects
When Start is called after a scene is loaded the following things have happened.
All Components have had Awake called.
In practice this means Awake should be used for internal initialisation. Awake is to set up internal variables. And it’s a good place to make all of your cached GetComponent calls.
Start is used for all of your external initialisation. So it’s the place where you do all initialisation that relies on other components.
Ah, I see. I think we were just misunderstanding each other. That’s the exact behavior I want: When averaged over time, it executes at a reliable frequency (such as the default 50 times per second), executing iterations back-to-back or not at all to achieve that average. I’m sure many people misunderstand it to mean that it literally tries to execute once every 20 milliseconds, but achieving that sort of reliability rarely matters anyway if your render frequency isn’t reliable.
I read that Awake() must be called first since it initialize the object, variables etc, no matter what disabled or enabled, meanwhile start() is called the moment the project begins and can rely in some objects, variable that have been initialized in Awake() function.
@corallotwins
No, Awake and Start are different. I would advice to place some Debug.Log inside those functions and experiment a bit. Try to enable/disable the gameObject or the script. You’ll figure out how these functions works.