- Home /
Scene.isLoaded not working properly.
So I have a very simple and small code that should do the following: Load Scene; After loaded, do stuff.
For making sure the scene would have enough time for loading, I called the LoadScene() function and made a loop under it that executes a million times (not kidding lol) checking if the scene has loaded. It never returns true. The code:
if (Input.GetMouseButtonDown(1))
{
SceneManager.LoadScene(sceneToLoad); //previously defined scene name (string)
for (int i = 0; i < 1000000; i++)
{
if (SceneManager.GetSceneByName(sceneToLoad).isLoaded)
{
//do stuff
break;
}
}
}
Apart from the one million runs loop, what's wrong with my code?
I just can't continue my project without this, if anyone who reads this has absolutely ANY idea, even if it probably doesn't work, post it here. Thanks.
Also, how can I formally wait until a function ends instead of having to make a loop that runs at the same time as the function?
Answer by Pangamini · Sep 24, 2021 at 09:25 PM
That loop does not cause any waiting, as it's executed synchronously. So the loading probably doesn't even start before you ask if it's done. Also you break the loop during the first execution. The correct way would be to check if the scene is loaded, then actually wait at least one frame before checking again. Check out coroutines or c# async/await pattern, those might help.
Tysm for the answer. The break is inside the if statement, wich always returns false. The loop never breaks. About executing things synchronously, what does that mean? I'm not any kind of programmer. Does that mean it runs on the same frame?
Well, the frame won't end before that method's call ends, so yes. You are right about the break, I missed that :-O
Ok, good to know about how frames deal with big loops, ty :)
Your answer
Follow this Question
Related Questions
Load Scene from .unity File 1 Answer
One Location Game (with Scenes Loaded and Disabled) 0 Answers
Loading Specific Scenes Post-Build 0 Answers
Multiple scenes 2 Answers
Adding to delegate with multiple instances of the same object 1 Answer