Wait, why did we use a coroutine here?

So I understand coroutines and I’ve used them before, but I’m not sure what we gained by using them here. I was curious and I slightly modified the code to not use a coroutine. When I run it, it seems to just just fine, just as I expected. I can’t notice any difference.

        private void OnTriggerEnter(Collider other)
        {
            if (other.gameObject.CompareTag("Player"))
            {
                DontDestroyOnLoad(gameObject);
                SceneManager.LoadSceneAsync(sceneBuildIndex);
                Debug.Log("Scene Loaded");
                Destroy(gameObject);
            }
        }

Lol. OK I get it.

I keep forgetting a basic principle of how Unity works. Some lines of code (e.g. ones that have to do with Scene Loading and destroying objects) don’t execute on the line they are written. They are basically just scheduled. So after requesting a Scene Load (sync or async) the immediate line that comes after it that executes should not expect that previous task completed.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms