- Home /
Unity Scene not loading.
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)". The scene is in the build settings so that shouldn't be a problem. Does anyone know what could be causing this?
I screen recorded what is going on click the link below to see. If you look in the hierarchy, you can see sometimes it is saying "not loaded" next to the scene name. Also, you can look in the console to see the debug.log pop up each time I click the button. Thank you so much for your help.
Answer by FlaSh-G · May 06, 2020 at 05:03 AM
I could not reproduce the issue in an otherwise empty scene (just a rigidbody cube in it to see whether the scene has reset). So my guess is that there's something else afoot here. Since Unity nowadays displays a special scene in the hierarchy for objects that are flagged with DontDestroyOnLoad, and since I do not see that scene in your video, I assume the idea of you simply having your visible panel survive the reload is off the table. It's also hard to come up with an idea involving some bad use of static variables here (their data would also survive the reload, and the reload seems to definitely happen).
My suggestion: Make a copy of your scene, start deleting objects one by one and test again after each deletion. Whenever this behavior stops happening is right after you deleted the responsible object. Alternatively, start a new scene from scratch and start adding all the elements you made one by one. Same idea.