??Whats wrong with this script??? HELP

I have a script that should:

  • On collision with a gameObject tagged ‘Cube’
  • Display a GUI box with some text saying ‘You got eaten by the cubes…’
  • And underneath that a GUI button with the text ‘Restart Game’
  • When you press the GUI button, it restarts the current level.

Unity just comes up with lots of compiler errors. Can anyone debug this code and give me a version that is correct?

Thankyou in advance…

 function OnCollisionEnter (hit : Collision)
{
     if(hit.transform.gameObject.name == "Cube")
     {
     
     function OnGUI() {
          GUI.Box (Rect (10, 10, 100, 20),"You got eaten by the cubes...");
     
	
	if GUI.Button(Rect(10,70,50,30),"Restart Game")
			Application.LoadLevel(Application.loadedLevel);
			
	}
	
  }
  
}

Maybe cause you have a function inside a function…

Take time to learn some basic programming, it would benefit you greatly. You have tried to declare the OnGUI function within the OnCollisionEnter function. You need to separate them into two functions and use a boolean to determine whether the GUI should show or not

function OnCollisionEnter (hit : Collision)
{
    if(hit.transform.gameObject.name == "Cube")
    {
        wasEaten = true;
    }
}

function OnGUI() 
{
    if(wasEaten)
    {
        GUI.Box (Rect (10, 10, 100, 20),"You got eaten by the cubes...");
 
        if (GUI.Button(Rect(10,70,50,30),"Restart Game"))
        {
             Application.LoadLevel(Application.loadedLevel);
        }
    }
}