Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

How can i make a restart button?

Started by thecapl Jul 6, 2021 at 4:20 PM 3 replies 4.9k views
Original Post
thecapl
thecapl

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. }
Shaarigan
Shaarigan

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
thecapl
thecapl

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

Machaira
Machaira

Read this

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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.