Reputation: 91
I'm trying to find a script for unity 2d. I need one that restarts the scene I've currently loaded. I've tried a couple from the internet, but none of them seem to work. I just need the basic script, not any way to trigger it because I can do that myself. Thanks in advance!
Upvotes: 9
Views: 32467
Reputation: 81
This is what I did:
void Start()
{
Button Button = this.GetComponent<Button>();
Button.onClick.AddListener(Reset);
}
public void Reset()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
It only takes in a build index
Upvotes: 8
Reputation: 433
I would recommend you to go through the SceneManager class in Unity Docs.
However, you can use this line to reload the current scene thus, restarting it:
SceneManager.LoadScene(SceneManager.GetActiveScene());
Hope that answers your questions.
Upvotes: 2