简体   繁体   中英

Why doesnt my scene finish loading Unity?

Hi I'm currently developing a game in Unity and I run into a small problem for some reason the fourth scene I'm creating(Level 3) gets stuck and doesnt finish loading preventing my game from transitioning from scene 2(Level 2) to scene 3(Level 3). I have a scenemanager script that manages these transitions and it works fine for all other scene transitions except in the case explained above. Does anyone know what I'm doing wrong? Below you will see the the code responsible for handeling the scene transitions:

This is my scenemanager script responsible for additively loading and unloading scenes:

public static class MySceneManager
{

    private static int lastLoadedScene = 0;

    public static void LoadScene(int index, MonoBehaviour caller)
    {
        ObjectPooler objP = new ObjectPooler();
        objP.ReleaseAll();
        caller.StartCoroutine(loadNextScene(index));
    }

    private static IEnumerator loadNextScene(int index)
    {
        var _async = SceneManager.LoadSceneAsync(index, LoadSceneMode.Additive);

        _async.allowSceneActivation = false;
        while (_async.progress < 0.9f)
        {

            yield return null;
        }

        _async.allowSceneActivation = true;

        while (!_async.isDone)
        {
            yield return null;
        }

        var newScene = SceneManager.GetSceneByBuildIndex(index);

        if (!newScene.IsValid()) yield break;


        SceneManager.SetActiveScene(newScene);


        if (lastLoadedScene >= 0) SceneManager.UnloadSceneAsync(lastLoadedScene);


        lastLoadedScene = index;

    }
}

This is the script from where I call the scene transition:

public class HeartScript : MonoBehaviour
{
    int HeartEnter = 1;
    void Start()
    {
        DontDestroyOnLoad(this.gameObject);
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        Scene scene = SceneManager.GetActiveScene();

        if (other.gameObject.CompareTag("White Ball"))
        {
            if (HeartIndicator.numOfHearts < 3)
            {
                Debug.Log("Entered COLLIDER");
                HeartIndicator.numOfHearts += 1;
            }

            if(scene.name == "Level 2" && HeartEnter == 1) 
            {
                MySceneManager.LoadScene(3, this);
                HeartEnter++;
            }

            this.gameObject.SetActive(false);

        }
    }
}

This may possibly happen due to:

  1. Scene 3 is not added to "Scenes in build". 在此处输入图像描述

  2. Or the game object holding co-routine script is not active.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM