0
votes

I have a button in my Unity2D game that the user can click and the scene will start over. On my button, I have a script attached with this code

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;

 public class Restart : MonoBehaviour
 {
     public void PlayAgain()
     {
         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
         Debug.Log("Play Again Works");
     }
 }

I know that the button is working and the Play Again function is working because the Debug.Log is working. However, when I click the button the scene doesn't restart. When I click the button, in the Unity hierarchy, next to the scene name it very briefly shows "(not loaded)". My scene is showing up in the Build settings. Does anyone know what could be causing this? Thanks for your help.

1
how exactly do you know the scene was not reloaded correctly?derHugo
It just simply didn't load. I'm not really sure how else to describe it.Taylor Reich
But how do you know? Anything not reset correctly? The scene is the same as the current one so how exactly does "didn't load" look like? Or better said what should happen if it was reloaded correctly?derHugo
The scene is supposed to start over and look like it did at the beginning of the level. When the button is clicked it just stays the same. The scene doesn't restart. (And I know that it should because a while ago, I had the SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex); put in a different part of the game and everything was completely fine.) In the hierarchy next to the scene name, it very briefly says (not loaded). Do you have any ideas on what I should do?Taylor Reich

1 Answers

-1
votes

First check that you have added the scene in your build settings. If that is not your problem try following code.

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