- Home /
Creating a Restart Button
Hi, I'm new to Unity, so I don't know much yet, but I've been creating a game which is filled with obstacles, and different paths, and you have to roll the ball around the whole area to collect Yellow Cubes. I'm trying to figure out how to create a Restart button, so that if the player messes up, or falls out, they can just play again. I haven't written any type of script for the restart button yet, so everybody's help is appreciated! (Currently, if you fall out of the boundaries, or want to restart, you have to close out the whole game, and start it again.)
Answer by JScotty · Oct 25, 2016 at 06:50 AM
Hi Mrkrisher,
First create an button UI -> Button Then create an class
C# ex:
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
public class Restart : MonoBehaviour {
public void RestartGame() {
SceneManager.LoadScene(SceneManager.GetActiveScene().name); // loads current scene
}
}
attatch this class to a GameObject and drag it in the Button -> OnClick component Select -> Restart->RestartGame
And your scene will restart.
Hope this helps for you
NOTE: In case you are using unity 5.3 or less use Application.LoadLevel(Application.loadedLevel); instead of SceneManager.LoadScene(Application.loadedLevel);
Okay, well I got the button on the right position and stuff. The problem is the script won't work, I just copy and pasted your exact script, but an error message pops up. (And btw I have Version 5.3.6) alt text
Application.loadedlevel is Deprecated. You should use Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene().name)
ins$$anonymous$$d.
His script says that right? He said to use Scene$$anonymous$$anager for 5.3 or more, and I used Scene$$anonymous$$anager because i am 5.3.6
If you are getting this error, probally you named your script from a different name than the one in the example and copied it.
Ok well, now the script attached to the empty child which I named Restart. I didn't attach it to anything else. No error message pops up, but when I enter play mode and click restart, nothing happens. @Namey5
Have you linked the restart method to the UIButtons OnClick event? https://unity3d.com/learn/tutorials/topics/user-interface-ui/ui-button?playlist=17111
The reset works but has a $$anonymous$$or problem. Everything resets fine, but when I reset, the directional light goes away, so the screen is pretty dark. It's probably a simple solution, but its one of the last steps to completing my game. Anybody know?
And btw Thank you guys so much for helping me. @Namey5 @JScotty @EpicPants
That only happens in the editor. When the game is built you won't notice it. It's just the lightmaps not being able to rebuild because you're in playmode.
Answer by Putteli · May 03, 2017 at 07:50 AM
Hi! If anyone has problems with timeScale, after restarting. You should add Time.timeScale = 1f; after that so it will return back to normal timeScale. For Example in my game when user dies time will be paused and if i just only did restart it was still paused.
Answer by mayankela21 · Dec 03, 2020 at 12:27 PM
Use That Is More Easier
SceneManager.LoadSceneAsync(SceneManager.GetActiveScene().buildIndex);
For Reastart