LincolnHartlaub77
LincolnHartlaub77

Reputation: 91

How do I restart the scene that I'm currently in, through script in unity 2d so that it resets my sprites?

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

Answers (3)

Nym
Nym

Reputation: 31

SceneManager.LoadScene(SceneManager.GetActiveScene().name);

Upvotes: 3

Joseph Hodes
Joseph Hodes

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

Madhusudan Sharma
Madhusudan Sharma

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

Related Questions