Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does the LoadScene() function in Unity change the scene?

Tags:

c#

unity3d

unity5

When you invoke the function LoadScene() does it immediately switch scenes, or does it simply signal that the scene needs to be changed? The documentation for LoadScene() does not say.

The specific example I am working with looks like this:

LoadScene(levelName);
ItemBox newBox = (ItemBox)Instantiate(...);

So with the above code, would the newBox exist in the scene we just loaded, or would it get created in the old scene, then destroyed when the new level is loaded.

like image 955
Umibozu Avatar asked Mar 28 '16 23:03

Umibozu


2 Answers

It's

 UnityEngine.SceneManagement.SceneManager.LoadScene("Gameplay");

and yes it does it "instantly" -- that is to say synchronously.

In other words, it "stops" at that line of code, waits until it loads the whole scene (even if that takes some seconds) and then the new scene begins.

Do not use the old command you mention in the question.

Note that Unity also has the ability to do async loading .. it "slowly loads the new scene in the background".

However: I encourage you to only use ordinary "LoadScene". All that matters is reliability and simplicity. Users simply don't mind if the machine just "stops" for a few seconds while a level loads.

(Every time I click "Netflix" on my TV, it takes some time for the TV to do that. Nobody cares - it is normal.)

But if you do want to load in the background, here's how...

public void LaunchGameRunWith(string levelCode, int stars, int diamonds)
    {
    .. analytics
    StartCoroutine(_game( levelCode, superBombs, hearts));
    }

private IEnumerator _game(string levelFileName, int stars, int diamonds)
    {
    // first, add some fake delay so it looks impressive on
    // ordinary modern machines made after 1960
    yield return new WaitForSeconds(1.5f);

    AsyncOperation ao;
    ao = UnityEngine.SceneManagement.SceneManager.LoadSceneAsync("Gameplay");

    // here's exactly how you wait for it to load:
    while (!ao.isDone)
        {
        Debug.Log("loading " +ao.progress.ToString("n2"));
        yield return null;
        }

    // here's a confusing issue. in the new scene you have to have
    // some sort of script that controls things, perhaps "NewLap"
    NewLap newLap = Object.FindObjectOfType< NewLap >();
    Gameplay gameplay = Object.FindObjectOfType<Gameplay>();

    // this is precisely how you conceptually pass info from
    // say your "main menu scene" to "actual gameplay"...
    newLap.StarLevel = stars;
    newLap.DiamondTime = diamonds;

    newLap.ActuallyBeginRunWithLevel(levelFileName);
    }

Note: that script answers the question of how you pass information "from your main menu" when the player hits play "on to the actual game play scene".

like image 200
Fattie Avatar answered Sep 17 '22 23:09

Fattie


Maybe it's changed. From the docs:

When using SceneManager.LoadScene, the loading does not happen immediately, it completes in the next frame.

I had been doing the exact same thing as the poster and it was instantiating in the old scene.

https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html

like image 41
Tyler Avatar answered Sep 21 '22 23:09

Tyler