🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

How can i make a restart button?

Started by
3 comments, last by Machaira 2 years, 11 months ago

Hello so in my game I'm working on it is vital to have a reset button in the game so the scene restarts but when I try to get it working nothing happens. I have no errors pop up and my code is linked with the ui button so I'm not quite sure what the problem is here. the ui button is under canvas then the name of the button is Restart, in my script its named Restart and in unity it shows no compiler errors but when I push the button it flashes but nothing else happens. so if anyone knows how to fix this please help.

  1. using UnityEngine;
  2. using UnityEngine.SceneManagement;
  3. using System.Collections;
  4. public class Restart : MonoBehaviour {
  5. public void RestartGame() {
  6. SceneManager.LoadScene(SceneManager.GetActiveScene().name); // loads current scene
  7. }
  8. }
Advertisement

2 possible causes for your issue

  1. You haven't connected your button properly. Set a breakpoint in Visual Studio or use a Log message to proof that your function is called
  2. SceneManager won't restart the game because your scene is already loaded and it does an early out. To prevent this, iterate through all scenes and unload them, then load the desired one again

ok so im a bit confused. how would I set a break point in visual studio and what does that mean exactly?

Read this

Former Microsoft XNA and Xbox MVP | Check out my blog for random ramblings on game development

This topic is closed to new replies.

Advertisement