Restart Button?

Hey,

I am trying to create a GUI button that resets the game including the score and the timer. I can draw the button but I’m not sure how to make it so that when it is clicked, the game resets. Thanks in advance for your help.

I have managed to create the restart button and have gotten the score to update but I still can’t get the timer to reset. Here is my code:

var gameLength : int = 30;

var timerText : GUIText;

function Update () {

var timeLeft : int = gameLength - Time.time;

	if ( timeLeft < 0 ) {

		timeLeft = 0;
		Time.timeScale = 0;
	
	}


	timerText.text = "Time Remaining: " +timeLeft;	

}

function OnGUI () {

	if ( GUI.Button( Rect( 1, Screen.height-25, 75, 25 ), "Restart" )) {

	Application.LoadLevel( "Pancake game" );
	PancakeCollector.score = 0;
}

}

I wrote this script for you! This creates a GUI button and when it’s clicked it loads a level of your choice. Customize it for your needs

function OnGUI()
{

    //makes a GUI button at coordinates 10, 100, and a size of 200x40
    if(GUI.Button(Rect (10,100,200,40),"Restart"))
    {
       //Loads a level
      Application.LoadLevel(//The name of the level you want to reload here);
    }
}

There you go!

EDIT: I’ll answer the rest of your question here:

I’m not sure If this works (I can’t test it because I’m not at home), but give it a try:

I think you have to add one line to your OnGUI:

function OnGUI () {

if ( GUI.Button( Rect( 1, Screen.height-25, 75, 25 ), "Restart" )) {

Application.LoadLevel( "Pancake game" );
PancakeCollector.score = 0;

//This should reset your timer back to 30
timeLeft = 30;

}

You could try this script:

function OnGUI(){
if(GUI.Label(Rect(500,500,250,50), "Reset")){
Application.Load("yourCurrentScene");
}
}

That would load the current scene. This would also reset any values that were made to any variables during the scene.

But if you store your scores in an external file, then you would also have to reset that too!!!

Hope this helps!!!

-Grady

Thanks so much for your help!! Using JUnityer’s script I can reset the scenery but the timer still continues from where it was last at and the score goes to 0 but as soon as a new point is gained, the points from before the restart are added. Any ideas on how to stop these problems?

void OnGUI(){
if(GUI.Label(Rect(500,500,250,50), “Reset”)){
Application.Loadlevel(scene_index);
}
}