How to script button "RestartGame"?

using UnityEngine;

public class RestartGame : MonoBehaviour

public void Button_Click()
{
Application.LoadLevel(Application.loadedLevel)
}

//Don’t working

Maybe instead try sceneManager.LoadScene(“whatever scene you want to restart at”) ?

1 Like

Application.LoadLevel and Application.loadedLevel are deprecated. It has been replaced with the new features in UnityEngine.SceneManagement.

Try this:

using UnityEngine;
using UnityEngine.SceneManagement;

public class RestartGame : MonoBehaviour
{
    public void Button_Click()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }
}
2 Likes

‘sceneManager.LoadScene does not exist in current context’

Working :slight_smile: but if I restart game my shaders does not working :hushed:

Pro tip (assuming you’re using visual studio), if you mouse over the error, it’ll suggest fixes, in this case you could of clicked on the first suggestion and it would’ve added the using directive in for you, just something that’s worth knowing! :stuck_out_tongue:

thanks @Tovey-Ansell

its SceneManager.LoadScene(SceneManager.GetActiveScene(“YourSceneName”).name);
Ok maybe not…

ADD THIS LINE! using UnityEngine.SceneManagement;

using UnityEngine.SceneManagement;

public class QToRestart : MonoBehaviour
{
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown ("space")) {
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
}```

This is not working.