Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SceneManager.SetActiveScene failed: scene is not loaded

I'm having a strange problem:

I load a scene additively, and it does show up, but Unity tells me:

ArgumentException: SceneManager.SetActiveScene failed; scene 'inventory' is 
not loaded and therefore cannot be set active
UnityEngine.SceneManagement.SceneManager.SetActiveScene (Scene scene)
PlayerScript.enableScene (System.String SceneName) (at 
Assets/PlayerScript.cs:83)
PlayerScript.OnFinishedLoadingAllScene () 

I'm not sure how this can happen. Does anybody see a mistake in my code?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

//First, load scene with SceneManager.LoadSceneAsync.
//Set allowSceneActivation to false so that the scene won't activate automatically after loading.

public class PlayerScript : MonoBehaviour
{
    public GameObject UIRootObject;
    private AsyncOperation _SceneAsync;
    private bool _bInventoryShown = false;
    private Scene PrevScene;

void Update()
{
    if (Input.GetKeyDown(KeyCode.I))
    {
        //player pressed I to show the inventory
        if (!_bInventoryShown)
        {
            //store the currently active scene
            PrevScene = SceneManager.GetActiveScene();

            //start loading the inventory scene
            StartCoroutine(loadScene("inventory"));
        }
        else
        {
            //player pressed I again to hide the inventory scene
            //so just set the previous scene as the active scene
            SceneManager.SetActiveScene(PrevScene);
        }

        _bInventoryShown = !_bInventoryShown;
    }
}
IEnumerator loadScene(string SceneName)
{
    AsyncOperation nScene = SceneManager.LoadSceneAsync(SceneName, LoadSceneMode.Additive);
    nScene.allowSceneActivation = false;
    _SceneAsync = nScene;

    //Wait until we are done loading the scene
    while (nScene.progress < 0.9f)
    {
        Debug.Log("Loading scene " + " [][] Progress: " + nScene.progress);
        yield return null;
    }

    //Activate the Scene
    _SceneAsync.allowSceneActivation = true;

    Scene nThisScene = SceneManager.GetSceneByName(SceneName);

    if (nThisScene.IsValid())
    {
        Debug.Log("Scene is Valid");
        SceneManager.MoveGameObjectToScene(UIRootObject, nThisScene);
        SceneManager.SetActiveScene(nThisScene);
    }
    else
    {
        Debug.Log("Invalid scene!!");
    }
}
}

I have also tried the following:

    AsyncOperation nScene = SceneManager.LoadSceneAsync(SceneName, LoadSceneMode.Additive);
    nScene.allowSceneActivation = false;

    //Wait until we are done loading the scene
    while (!nScene.isDone)
    {
        Debug.Log("Loading scene " + " [][] Progress: " + nScene.progress);
        yield return null;
    }
    Debug.Log("Scene was loaded!");

The last log ("Scene was loaded") is never called.

like image 313
tmighty Avatar asked Oct 21 '25 10:10

tmighty


1 Answers

from AsyncOperation.progress (also see the example there)

Return an operation's progress. (Read Only) This returns how close the operation is to finishing. The operation is finished when the progress float reaches 1.0 and isDone is called. If you set allowSceneActivation to false, progress is halted at 0.9 until it is set to true. This is extremely useful for creating loading bars.

you should already set it after the while but than anyway wait again

// ...

while (nScene.progress < 0.9f)
{
    Debug.Log("Loading scene " + " [][] Progress: " + nScene.progress);
    yield return null;
}

//Activate the Scene
_SceneAsync.allowSceneActivation = true;

while (!nScene.isDone)
{
    // wait until it is really finished
    yield return null;
}

//...
like image 156
derHugo Avatar answered Oct 26 '25 12:10

derHugo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!