- Home /
Loading an additive scene once and only once?
I want to be able to start from different scenes in the editor, and have a specific scene that will always load, regardless. In this specific case it is a scene that loads and tracks player data that matters in my menu scene as well as in my main game scene. I want to start either, depending on what I am working on at that moment, and still have the setup scene do its thing and then persist (with DontDestroyOnLoad) when I switch scenes while running.
It seems like this should work if I put it on some dummy prefab in all my scenes with this on it:
void Awake()
{
if(!SceneManager.GetSceneByName("Setup").isLoaded)
{
SceneManager.LoadScene("Setup", UnityEngine.SceneManagement.LoadSceneMode.Additive);
}
GameObject.Destroy(gameObject);
}
But it doesn't! I end up with a new copy of the "Setup" scene whenever I load a scene with this prefab in it.
Replacing that conditional with:
if(!GameObject.Find("GameSetup"))
works, (and is how I used to do it with the old system), but is sort of dumb, since I am just checking an arbitrary object that happens to be in that scene.
Any help would be awesome!
Additionally, using the second method causes the hierarchy view scene labels to disappear, which seems like incorrect behavior. Either case unloads the first scene and loads the second one, but if the "Setup" scene isn't also loaded again, it breaks.
I had this same problem. "isLoaded" does not work how you would expect when run within Awake methods. In case it's useful to anyone, this is the hack that I used:
public void Awake()
{
if (ShouldLoadNextScene())
{
Scene$$anonymous$$anager.LoadScene("$$anonymous$$yNextScene", LoadScene$$anonymous$$ode.Additive);
}
}
bool ShouldLoadNextScene()
{
return GetSceneIndex(this.gameObject.scene) == Scene$$anonymous$$anager.sceneCount - 1;
}
int GetSceneIndex(Scene scene)
{
for (int i = 0; i < Scene$$anonymous$$anager.sceneCount; i++)
{
if (Scene$$anonymous$$anager.GetSceneAt(i) == scene)
{
return i;
}
}
throw new Exception();
}
Your answer
Follow this Question
Related Questions
Waiting Time Difference Between 720p and 1080p 0 Answers
Loading Specific Scenes Post-Build 0 Answers
Scene.isLoaded not working properly. 1 Answer
Load Scene from .unity File 1 Answer