Farooq
Farooq

Reputation: 35

Scene is not Loading

There are 9 scenes in total. All are Added in "Build Settings". 7 scenes out of 9 are loading, but when I try to load other 2 scenes it kept saying:

Scene 'SceneName' couldn't be loaded because it has not been added to the build settings or the AssetBundle has not been loaded. To add a scene to the build settings use the menu File->Build Settings... UnityEngine.SceneManagement.SceneManager:LoadSceneAsync(String, LoadSceneMode) MoreMountains.CorgiEngine.c__Iterator0:MoveNext() (at Assets/CorgiEngine/Common/Scripts/Managers/LoadingSceneManager.cs:88) UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator) MoreMountains.CorgiEngine.LoadingSceneManager:Start() (at Assets/CorgiEngine/Common/Scripts/Managers/LoadingSceneManager.cs:67)

Here is my "Scene In Build" Image

I have already added the scene in the scene manager but it keeps showing this error. I have tried so much. Please help me to sort out the problem. Here is my "LoadingSceneManager" script:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using MoreMountains.Tools;
using UnityEngine.SceneManagement;

namespace MoreMountains.CorgiEngine
{   
public class LoadingSceneManager : MonoBehaviour 
{
    [Header("Binding")]
    /// The name of the scene to load while the actual target scene is 
    loading (usually a loading screen)
    public static string LoadingScreenSceneName="LoadingScreen";

    [Header("GameObjects")]
    /// the text object where you want the loading message to be displayed
    public Text LoadingText;
    /// the canvas group containing the progress bar
    public CanvasGroup LoadingProgressBar;
    /// the canvas group containing the animation
    public CanvasGroup LoadingAnimation;
    /// the canvas group containing the animation to play when loading is complete
    public CanvasGroup LoadingCompleteAnimation;

    [Header("Time")]
    /// the duration (in seconds) of the initial fade in
    public float StartFadeDuration=0.2f;
    /// the speed of the progress bar
    public float ProgressBarSpeed=2f;
    /// the duration (in seconds) of the load complete fade out
    public float ExitFadeDuration=0.2f;
    /// the delay (in seconds) before leaving the scene when complete
    public float LoadCompleteDelay=0.5f;

    protected AsyncOperation _asyncOperation;
    protected static string _sceneToLoad = "";
    protected float _fadeDuration = 0.5f;
    protected float _fillTarget=0f;
    protected string _loadingTextValue;

    /// <summary>
    /// Call this static method to load a scene from anywhere
    /// </summary>
    /// <param name="sceneToLoad">Level name.</param>
    public static void LoadScene(string sceneToLoad) 
    {       
        _sceneToLoad = sceneToLoad;                 
        Application.backgroundLoadingPriority = ThreadPriority.High;
        if (LoadingScreenSceneName!=null)
        {
            SceneManager.LoadScene(LoadingScreenSceneName);
        }
    }

    /// <summary>
    /// On Start(), we start loading the new level asynchronously
    /// </summary>
    protected virtual void Start() 
    {
        _loadingTextValue=LoadingText.text;
        if (_sceneToLoad != "")
        {
            StartCoroutine(LoadAsynchronously());
        }
    }

    /// <summary>
    /// Every frame, we fill the bar smoothly according to loading progress
    /// </summary>
    protected virtual void Update()
    {
        LoadingProgressBar.GetComponent<Image>().fillAmount = MMMaths.Approach(LoadingProgressBar.GetComponent<Image>().fillAmount,_fillTarget,Time.deltaTime*ProgressBarSpeed);
    }

    /// <summary>
    /// Loads the scene to load asynchronously.
    /// </summary>
    protected virtual IEnumerator LoadAsynchronously() 
    {
        // we setup our various visual elements
        LoadingSetup();

        // we start loading the scene
        _asyncOperation = SceneManager.LoadSceneAsync(_sceneToLoad,LoadSceneMode.Single );
        _asyncOperation.allowSceneActivation = false;

        // while the scene loads, we assign its progress to a target that we'll use to fill the progress bar smoothly
        while (_asyncOperation.progress < 0.9f) 
        {
            _fillTarget = _asyncOperation.progress;
            yield return null;
        }
        // when the load is close to the end (it'll never reach it), we set it to 100%
        _fillTarget = 1f;

        // we wait for the bar to be visually filled to continue
        while (LoadingProgressBar.GetComponent<Image>().fillAmount != _fillTarget)
        {
            yield return null;
        }

        // the load is now complete, we replace the bar with the complete animation
        LoadingComplete();
        yield return new WaitForSeconds(LoadCompleteDelay);

        // we fade to black
        GUIManager.Instance.FaderOn(true,ExitFadeDuration);
        yield return new WaitForSeconds(ExitFadeDuration);

        // we switch to the new scene
        _asyncOperation.allowSceneActivation = true;
    }

    /// <summary>
    /// Sets up all visual elements, fades from black at the start
    /// </summary>
    protected virtual void LoadingSetup() 
    {
        GUIManager.Instance.Fader.gameObject.SetActive(true);
        GUIManager.Instance.Fader.GetComponent<Image>().color=new Color(0,0,0,1f);
        GUIManager.Instance.FaderOn(false,ExitFadeDuration);

        LoadingCompleteAnimation.alpha=0;
        LoadingProgressBar.GetComponent<Image>().fillAmount = 0f;
        LoadingText.text = _loadingTextValue;

    }

    /// <summary>
    /// Triggered when the actual loading is done, replaces the progress bar with the complete animation
    /// </summary>
    protected virtual void LoadingComplete() 
    {
        LoadingCompleteAnimation.gameObject.SetActive(true);
        StartCoroutine(MMFade.FadeCanvasGroup(LoadingProgressBar,0.1f,0f));
        StartCoroutine(MMFade.FadeCanvasGroup(LoadingAnimation,0.1f,0f));
        StartCoroutine(MMFade.FadeCanvasGroup(LoadingCompleteAnimation,0.1f,1f));

    }
}

}

Upvotes: 2

Views: 4932

Answers (7)

Luke
Luke

Reputation: 153

For some reason, I had all the scenes added in Build Settings properly, but it was still throwing an error when running the game in the editor (no problem when built for android).

I just removed all scenes from the Build Settings and added them back again. No problem at all after that.

Upvotes: 0

Hexodus
Hexodus

Reputation: 12965

I know this question is old but my answer might be still valuable to some people having this problem. The scenes you add to your build should avoid more exotic characters like german umlauts "äöÜß" or simmilar. And it'll be better if you avoid whitespace characters as well - as they might contain some other sign then you suspect and therefore cause trouble. Especially when you did a copy&paste to name them. So better stick to plain ASCII letters and replace all blanks with hyphen or underscore.

Upvotes: 1

Zeeshan Arif
Zeeshan Arif

Reputation: 517

Please check your function "LoadScene".

I'm not sure what exact problem is but from your logs the problem is in "LoadScene" function.

Upvotes: 0

Woltus
Woltus

Reputation: 448

It's look like You try to load scene with name: 'SceneName'.

  1. Check if You have proper values on functions parameters.
  2. Try to load problematic scenes using indexes instead of names.

Upvotes: 0

Maaz Irfan
Maaz Irfan

Reputation: 81

or you can use by their index values its much easier approach rather then using string

SceneManager.LoadScene(0);

and then add all those scene by opening build setting window

Upvotes: 1

Muhammad Afzaal
Muhammad Afzaal

Reputation: 66

GO File-> and Build Setting make sure you have all the Scene in the box.enter image description here

Upvotes: 0

Nino9612
Nino9612

Reputation: 958

Have you added your scene which you try to load into the build settings "Scenes In Build" Section? This is required to switch to any scene inside your project using the scenemanager.

enter image description here

Upvotes: 4

Related Questions