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: Unity - Scripting API: MonoBehaviour.Awake()
I would recommend to read this question, where runevision explains the difference between Awake, Start, Update and so on: What is the general use of Awake(), Start(), Update(), FixedUpdate(), LateUpdate()? - Unity Answers
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.