0

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.

5
  • how exactly do you know the scene was not reloaded correctly? Commented Apr 30, 2020 at 6:08
  • It just simply didn't load. I'm not really sure how else to describe it. Commented Apr 30, 2020 at 17:55
  • 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? Commented Apr 30, 2020 at 22:44
  • 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? Commented May 1, 2020 at 2:29
  • You may just continuously be reloading the scene. For example if you reload the scene because the player died, make sure to reset the player health when reloading the scene so it doesn't try to reload again on the next frame. Commented Jan 22, 2022 at 21:18

1 Answer 1

-1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.