0
votes

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

        }
    }
}
1
possibly because you set gameobject.active to false and then it tries to start a co routine on it.BugFinder
@BugFinder Thanks man that seems to do the trickMaurice Bekambo
Check the tag descriptions before you add them. The game-engine tag is only appropriate when asking questions about writing your own engine, not when working with one. I would also remove Unity from the title if I could, because it is a tag and already tagged in the tags, but your title is considered bad (likely a duplicate) by Stack Overflow, indicating that you did not look at the possible duplicates.Draco18s no longer trusts SE

1 Answers

2
votes

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.