- Home /
What is the difference Between Awake and Update Function?
Hi, i am kind of confused here.Maybe it is an odd question but i need to ask cause i am getting different results. So here is my basic deltaPosition code that detects the delta position of an object during last frame.There are two different results and that is where i get confused.When i define the variables in awake function my delta result in Vector3.zero but when i define my variables in a custom function that will be checked in update function every frame,everything is fine as well as correct delta values.Pls tell me what is wrong with it,why everything gets messy while i define the variables in awake function???
Thank you!
#pragma strict
var firstPos : Vector3;
var secPos : Vector3;
var deltaPos : Vector3;
var target : Transform;
function Update () {
target.Translate(0.1, -0.2, 0);
DeltaPos ();
}
function DeltaPos () {
firstPos = target.position;
yield;
secPos = target.position;
deltaPos = secPos - firstPos;
}
#pragma strict
var target : Transform;
function Update () {
target.Translate(0.1, -0.2, 0);
DeltaPos ();
}
function DeltaPos () {
var firstPos = target.position;
yield;
var secPos = target.position;
var deltaPos = secPos - firstPos;
}
First code results the delta value in (0,0,0) and the second code results in the correct delta value which is (0.1, -0.2, 0).
What is wrong??
CORRECT DELTA VALUES!

WRONG DELTA VALUES!

Here is the scene if you want to check it yourself;
What is the difference between these scripts. The only difference I can see is that the first has three unused Vector3s defined. (I am assu$$anonymous$$g 'firstPos' is actually 'first'?)
Oh i am sorry.I edited it now.Now would you take a look at it again?
Vio$$anonymous$$yma you are right! I mis typed the variables.Sorry about it.
Answer by stardust · Nov 16, 2013 at 12:21 PM
For your question about the difference between Awake and Update, you could read through the description in the Scripting reference, for example the Awake Function: http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.Awake.html
I would recommend to read this question, where runevision explains the difference between Awake, Start, Update and so on: http://answers.unity3d.com/questions/10189/what-is-the-general-use-of-awake-start-update-fixe.html
But your problem actually is caused because you call a coroutine which is executed over several frames (in your case two) in every frame via the Update function.
#pragma strict
var firstPos : Vector3;
var secPos : Vector3;
var deltaPos : Vector3;
var target : Transform;
function Update () {
target.Translate(0.1, -0.2, 0);
DeltaPos ();
}
function DeltaPos () {
firstPos = target.position;
yield;
secPos = target.position;
deltaPos = secPos - firstPos;
}
In your first code example your variables are instance variables, which means, every function in your script can access it. So what happens is:
In frame 1 you call the function DeltaPos. In DeltaPos the targets position gets stored in firstPos. Then DeltaPos waits for the next frame. In frame 2 in the Update function you again call DeltaPos. So you now have two DeltaPos running. DeltaPos2 now stores the targets position of frame 2 in firstPos, while DeltaPos1 runs its second part and compares firstPos (already overwritten by DeltaPos2) with secPos (the targets position in frame 2) which is the same and therefore equals 0.
#pragma strict
var target : Transform;
function Update () {
target.Translate(0.1, -0.2, 0);
DeltaPos ();
}
function DeltaPos () {
var firstPos = target.position;
yield;
var secPos = target.position;
var deltaPos = secPos - firstPos;
}
In your second example the variables firstPos, secPos and deltaPos are declared in DeltaPos itself, which means only DeltaPos itself can access it. This will happen in your script:
In frame 1 the function DeltaPos is called. It then stores the targets position in his own firstPos(1). DeltaPos now waits for frame 2. In frame 2 you call another DeltaPos. DeltaPos2 then stores the targets position in frame 2 in its own firstPos(2). DeltaPos1 compares now the value in his firstPos(1), with the targets position in frame 2. So you have two firstPos variable, one for each DeltaPos you called.
Because you call DeltaPos in every frame anyways, you don't need the yield. I would recommend you to change your code to something like this:
#pragma strict
var firstPos: Vector3;
var secPos: Vector3;
var deltaPos: Vector3;
var target: Transform;
function Start () {
firstPos= target.position;
}
function Update () {
target.Translate(0.1, -0.2, 0);
DeltaPos();
}
function DeltaPos() {
secPos= target.position;
deltaPos= secPos- firstPos;
firstPos= target.position;
}
At first you store the targets position in the Start function for the first time. DeltaPos now all does in one Frame. When called, it takes firstPos stored in the last Frame, compares it with the targets position in the current frame. When it has done all the calculations, in your case if you have deltaPos, it overwrites firstPos, as a preparation for the next frame.
Great explanation! Thank you a lot man for the time of your effort writing it.Im glad!
Answer by ffxz7ff · Nov 16, 2013 at 10:44 AM
What is the difference Between Awake and Update Function?
As far as I know, Awake() gets called once when the object is created, and Update() runs once per frame. But I think your code example doesn't show the problem so far.
I edited the code now, i mis-typed the variables now it is correct. $$anonymous$$y problem is that when i declare the variables in Awake function delta value is result in a wrong way which is Vector3.zero. But when i declare my variables in a custom function it shows the correct delta values which is (0.1, -0.2, 0). $$anonymous$$y question is why these two results are different? What happened when i defined my variable in awake function? Why it show the wrong values?
why everything gets messy while i define the variables in awake function???
If you actually declared the variables inside the Awake() function, then they should only be accessible within that function. Declaring your variables as part of the class might fix your problem.
Hmm but we can access that variables in any function not just in the awake function? When i top at top of the script like var a : float; I can access and change that a variable in any function inside. I don't understand it why these 2 scripts has different results? The only difference is that one of the script variables defined at top of the script which is awake and in the other script it was defined in a custom fucntion. The only thing is that the place of the variables where it is declared is different. Both of the code is actually doing the same thing.
Do you override firstPos, secondPos and deltaPos anywhere else in the script? Do you perhaps declare them twice, in the class and in Awake() function?
No i don't override or change them anywhere else in the script.I can show you the scene file if anyone is interested?
http://s3.dosya.tc/server13/$$anonymous$$V1hkN/NewUnityProject.rar.html
Your answer