[SOLVED] Fully reset current scene??

hello,

i’m using this for reset current scene:

private IEnumerator Reload()
{
yield return new WaitForSeconds(6);
Resources.UnloadUnusedAssets();
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex, LoadSceneMode.Single);
}

but seems that some of variables doesn’t fully reset like as first scene opening situation!?!

any ideas?
thanks.

Perhaps Reloading the scene works better:

 SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
3 Likes

???
did you read my code?

2 Likes

Reload the current scene using Unity - Scripting API: SceneManager
This will reset everything but be aware of the following caveats:

  • Scenes with realtime GI will need auto turned off (manually bake them) in editor because otherwise Unity won’t save this temporary automatically generated lighting.
  • static variables will stick around and survive reloads as well as anything flagged with HideFlags to not unload

Why is it all squashed on one line out of context instead of being the full proper code ?

i don’t understand?

what is difference between my code and @ClaudiaTheDev reply?

Yeah and i didnt see that is a single long line and i can scroll to see the whole part. So sorry for that!

1 Like

ok … for now?

ok … found that :]

private IEnumerator Reload()
{
yield return new WaitForSeconds(6);
SceneManager.UnloadSceneAsync(SceneManager.GetActiveScene().buildIndex);
Resources.UnloadUnusedAssets();
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex, LoadSceneMode.Single);
}
1 Like

rude…

5 Likes

[BUMP]
I only have one scene, so when I reload it using:

UnityEngine.SceneManagement.SceneManager.LoadScene(0);

A lot of my variables are not reset. I’d like this to be like the first time the scene is loaded/run, but it’s not doing that. Maybe I need a second scene that can stay loaded that will load and reload the scene for resets?

Anyone know how to make a scene reload as if this were the first time? Resetting all the variables? 0

I just spent a couple hours on this one myself and found a working solution for my needs I wanted to share for the people of the future seeking help as I am sure this years old issue is no longer relavant for original poster it may be valued for future searchers.

So for me I have a single DoNotDestroyOnLoad Object called NetworkManager so I destroy that first before unloading the scene and then reloading.

The key I find is using a second scene to handle this so everything is completely removed before being added back in so everything is fresh. I just added the following script to a single gameobject in it’s own scene and call the static function ReloadScene(); and all seems to work as desired for no restart reloads …

    public class LoadingScene : MonoBehaviour
    {

        private void Start()
        {
            StartCoroutine(ReloadMainScene());
        }

        private IEnumerator ReloadMainScene()
        {
            // Find and destroy the Main Scene singleton
            if (NetworkManager.singleton != null)
            {
                Destroy(NetworkManager.singleton.gameObject);
            }
            
            // Unload the current scene
            yield return SceneManager.UnloadSceneAsync(SceneManager.GetActiveScene().name);

            yield return  Resources.UnloadUnusedAssets();
            // Load the main scene again
            yield return SceneManager.LoadSceneAsync(0);
            
        }

        public static void ReloadScene()
        {
            SceneManager.LoadScene(1);
        }
    }

hope this helps someone else. cheers.