Videogames Development – Unity – Delay Execution Time

yield WaitForSeconds

#pragma strict

function Start()
{
// Prints 0
print (Time.time);
// Waits 5 seconds
yield WaitForSeconds (5);
// Prints 5.0
print (Time.time);    
}

function Update()
{

}

NOTICE:
yield WaitForSeconds (5); -> yield (dare la precedenza) Aspetta per Secondi 5

Invoke

#pragma strict
 
function Start()
{
    // Invoke (function name, delay time in seconds)
    Invoke ("myFunction", 2);
    Invoke ("myFunctionTwo", 4);
}
    
function myFunction()
{
    Debug.Log("Delay time of 2 seconds");
}

function myFunctionTwo()
{
    Debug.Log("Delay time of 4 seconds");
}

Invoke Repeat

#pragma strict
 
var x: int;

function Start()
{
    // InvokeRepeiting (function name, delay time in seconds, repeat every x seconds...)
    InvokeRepeating("myFunction", 2, 1);
}

function myFunction()
{
    var x = x++;
    Debug.Log("Invoked " + x);
}

The result is:

Invoked 0
Invoked 1
Invoked 2
Invoked 3
… to infinity and beyond!