Unity freezes when changing scenes

Hello everyone, I just recently got this problem without changing anything related to it at all, the whole app freezes when I change scene, but only… sometimes? It also occurs when I build it. Only 1 scene works always, multiple scenes crash the app, but only sometimes?

Video:
unity freezing - YouTube

Here is the script I used for the portal, LoadSceneAsync, LoadScene, still the same problem. Changed string to int, still crash. I don’t know what I’m doing wrong anymore. Any solutions are appreciated!

using UnityEngine;
using UnityEngine.SceneManagement;
public class Portal : Triggerable
{
    public string[] sceneNames;

    protected override void OnTriggerEnter2D(Collider2D other)
    {
        string sceneToLoad = sceneNames[Random.Range(0, sceneNames.Length)];

        Debug.Log(sceneToLoad);

        if (other.name == PlayerData.name)
        {
            SceneManager.LoadSceneAsync(sceneToLoad);
        }
    }

}

It looks to me as if you are not handling the LoadSceneAsync method correctly. I could be wrong, but im sure it needs an iterator / loop as seen in the docs, to fully utilize the async method. Not to mention, but im fairly sure i read somewhere, that the Editor does not respect the true smooth loading loop without a ‘hitch’. In my experiences, the editor always has a slight pause using ASync loading, but a build seems to not have that issue.

To top things off, various things like heavy enable / start methods on pre-existing scene objects seem to signifigantly affect the overall load time.

Im no elite unity guy, so maybe someone else can correct, or expand upon this.

I used LoadScene prior to this and it still freezes for me, I tried to iterate over and over and nothing has worked out. It’s the single line

SceneManager.LoadSceneAsync(sceneToLoad)

that is the problem but I can’t wrap my heads about why. The code is really simple, no infinite loopings that could occur.

I checked my source control history and before I notice this bug and everything was working perfectly. I just added some new enemies that don’t relate to this bug whatsoever.

I’m guessing one of your new enemies has an infinite loop. Prove me wrong! :slight_smile:

EDIT: or even more likely, if you’re doing random enemy placement, your placement code fails to handle the case where there isn’t enough room to fit all enemies in an area, and it keeps trying indefinitely, another infinite loop.

Unity will lock up 100% of the time EVERY millisecond your scripting code is running.

Nothing will render, no input will be processed, no Debug.Log() will come out, no GameObjects or transforms will appear to update.

Absolutely NOTHING will happen… until your code either:

  • returns from whatever function it is running

  • yields from whatever coroutine it is running

As long as your code is looping, Unity isn’t going to do even a single frame of change. Nothing.

No exceptions.

“Yield early, yield often, yield like your game depends on it… it does!” - Kurt Dekker

1 Like

THANK YOU!!! Your advices helped me finding out the real cause of my problem, and understanding everything. My wave spawner is the real culprit. The reason it freeze is because it is trying to add enemies when there are no enemies left to add, and it doesn’t stop because the total cost of enemies in a single spawner doesn’t reach its full cost yet. And it doesn’t reach it full cost because the max is 15 but the enemie’s lowest cost is 2 so sometimes it just freezes but sometimes it randomly reaches the max value successfully because of other enemies costs . I followed a tutorial and forgot that it uses a while loop and got cocky because I thought I never used a while loop before, my bad! I will remember this for the future.

Again, thank you!

1 Like

Nice! Soon you will have to change your user name from nondev into dev !

1 Like

It needs an iterator.
Documentation:
https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadSceneAsync

I copy pasted the IEnumerator from the example and it worked flawlessly.