Beginner needs help with a scripting question please

Can anyone show me how to get this script working with the “commented out” lines included. It works as it is but I can’t manage to get it to work with a delay before loading the level.

I’ve tried different ways to set the script out, I’m still only a beginner.

var StartTime : float;
var customSkin:GUISkin;

function OnGUI (){   
    if(GUI.Button(Rect(0,0,100,50),"Play Game")){  
        //  StartTime = Time.time + 3;
        //  if (Time.time-StartTime >= 3);{
        Application.LoadLevel ("game") ;
    } 
}

The code inside if(GUI.Button(…)) will be executed only at the exact frame you release the mouse at the end of the clic. If you want to wait, you must do it somewhere else.

There is several way. The simplest is to use Invoke, with 3 as the second parameter. Declare a function that only call LoadLevel and give it’s name as a string for parameter one. Like this :

function OnGUI(){
    if(GUI.Button(Rect(0,0,100,50),"Play Game")){  
        Invoke( "WaitAndLoad", 3.0 );
    } 
}

// You can choose another name, but it must match with the one in Invoke
function WaitAndLoad(){
    Application.LoadLevel ("game") ;
}

There is another way, just a tiny bit more complicated. For now stick with this one, and keep the words coroutine and yield in a corner of your mind for when you feel more comfortable.