Why doesnt my scene finish loading Unity?

Maurice Bekambo

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);

        }
    }
}
Swati

This may possibly happen due to:

  1. Scene 3 is not added to "Scenes in build". enter image description here

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related