Unity SceneManager loads scene, Start / Awake do not run on load

3.6k Views Asked by At

I have a StartupScript that i want to run every time when a scene (re)loads.

When I load Scene 1 the script runs.
When I load Scene 2 the script runs.
When I go back to Scene 1 the script does not run.

How can i force the StartupScript to run every time the scene is loaded?

The script is present in each scene, on a prefab StartScriptHolder.

I'm looking at SceneManager: maybe by making and deleting named instances of Scenes I can force the StartupScript to be generated every time?
It seems very inefficient to create and discard complete Scenes just for the purpose of running a single script.

1

There are 1 best solutions below

1
On

I don't know how are you handling the swap between scenes. Have you tried using UnloadSceneAsync from SceneManager? (https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.UnloadSceneAsync.html)

I am working in a project where I have to do the same. I unload a scene and if I load it again, I need to run the Start function again.

    SceneManager.LoadScene("SceneName", LoadSceneMode.Additive); //Loading the new scene
    SceneManager.UnloadSceneAsync(val); // Unloading current scene

In this case what I am doing is load the new scene by its name and in additive mode (this is a requirement I have but you can load it using another mode. After that I use the function I suggest you tu use which needs as parameter the ID of the scene. You can find this ID in build settings window. BuildSettings Window