scene loading errors

in some scenes the loader returns errors.

Exception encountered in operation UnityEngine.ResourceManagement.ResourceManager+CompletedOperation`1[UnityEngine.ResourceManagement.ResourceProviders.SceneInstance], result='', status='Failed': Exception of type 'UnityEngine.AddressableAssets.InvalidKeyException' was thrown., Key=[c7ac54c20ec18db4fb2623d1e20d1f6d], Type=UnityEngine.ResourceManagement.ResourceProviders.SceneInstance
UnityEngine.AddressableAssets.Addressables:LoadSceneAsync(Object, LoadSceneMode, Boolean, Int32)
<_LoadinScene>d__12:MoveNext() (at Assets/SceneLoader.cs:69)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
SceneLoader:OnClickLoad(Int32) (at Assets/SceneLoader.cs:64)
<>c__DisplayClass9_0:<MakeSceneButtons>b__0() (at Assets/SceneLoader.cs:53)
UnityEngine.EventSystems.EventSystem:Update() (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/EventSystem.cs:377)

anyone knows how to fix that?

the code:

using System.Collections;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.ResourceManagement.ResourceProviders;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class SceneLoader : MonoBehaviour
{
    static SceneLoader _instance;
    public AssetReference[] scenesToLoad;
    public Button loadButtonPrefab;
    public Transform loadButtonParent;
    public Slider loadProgress;
    SceneInstance? _currentLoadedScene = null;
    public GameObject loadingPresentationContent;

    void Awake()
    {
        SetupSingleton();
        MakeSceneButtons();
        loadProgress.gameObject.SetActive(false);
        loadingPresentationContent.SetActive(true);
    }

    void SetupSingleton()
    {
        if (_instance)
            Destroy(gameObject);
        else
        {
            _instance = this;
            DontDestroyOnLoad(gameObject);
        }
    }

    void MakeSceneButtons()
    {
        for (int i = 0; i < scenesToLoad.Length; i++)
        {
            if (scenesToLoad[i].RuntimeKeyIsValid() == false)
                continue;
            GameObject g;
            if (i == 0)
                g = loadButtonPrefab.gameObject;
            else
                g = Instantiate(loadButtonPrefab.gameObject, loadButtonParent);
            var b = g.GetComponent<Button>();
            var sceneName = scenesToLoad[i].ToString();
            b.GetComponentInChildren<Text>().text = sceneName;
            var tmpToMakeLambdaSelfContained = i;
            b.onClick.AddListener(() => OnClickLoad(tmpToMakeLambdaSelfContained));
        }
    }

    AsyncOperationHandle<SceneInstance> loadAsync;

    public void OnClickLoad(int index)
    {
        if (loadAsync.IsDone == false) return;
        loadButtonParent.gameObject.SetActive(false);
        loadingPresentationContent.SetActive(true);
        StartCoroutine(_LoadinScene(index));
    }

    IEnumerator _LoadinScene(int index)
    {
        loadAsync = Addressables.LoadSceneAsync(scenesToLoad[index], LoadSceneMode.Single);
        loadProgress.gameObject.SetActive(true);
        AsyncOperationHandle<SceneInstance>? unloadingAsync = null;
        // unload the current scene if there is one
        if (_unload && _currentLoadedScene != null)
        {
            unloadingAsync = Addressables.UnloadSceneAsync(_currentLoadedScene.Value);
        }
        // updating the ui bar
        while (loadAsync.IsDone == false || (loadAsync.PercentComplete <= 0.9f && unloadingAsync != null))
        {
            loadProgress.value = loadAsync.PercentComplete;
            yield return null;
        }
        loadProgress.gameObject.SetActive(false);
        loadButtonParent.gameObject.SetActive(true);
        _currentLoadedScene = loadAsync.Result;
        loadingPresentationContent.SetActive(true);
    }

    bool _unload = false;

    public void GUISetUnload(Toggle t)
    {
        _unload = t.isOn;
    }
}

Hm, not really sure why that’s breaking like that, but maybe try changing Addressables.LoadSceneAsync(scenesToLoad[index] to scenesToLoad[index].LoadSceneAsync

Also, if you’re loading a scene in single mode, you don’t need to explicitly unload any scenes, as that happens automatically.

1 Like

cool thx
i found out that an asset reference wasn’t a scene
unfortunately Scene cannot be used as AssetReferenceT