Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Scene management: verify if a scene exists?

Discussion in 'Scripting' started by Marscaleb, Jun 12, 2020.

  1. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    992
    Is there any way to verify if a given string actually represents a legitimate scene?

    Currently if I call SceneManager.LoadScene(levelName) and that level name doesn't refer to a legitimate scene that has been added to the scenes in the build, then this triggers an error and my game stops because it is expecting something to load that never will.

    Is there any way to just test a given string to see if it is a legitimate level or not, rather than committing to load something that doesn't exist?
    I can't seem to find a function for this within the scene manager.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
  3. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    992
    Okay, I'm not getting proper results from the "isValid" check.

    I tried
    if (SceneManager.GetSceneByName(loadThisLevel).IsValid())
    but it always results in a false statement.
    The levels I am checking are legitimate scenes, and if I bypass the above code they load just fine.
     
  4. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,913
    Because it only works if the scene is loaded. You don't have name if you don't have the scene loaded.
    https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.GetSceneByName.html
    You can use the scene path or scene build index to check scenes in the valid scenes in the build settings:
    Code (CSharp):
    1.         for (var i = 0; i < SceneManager.sceneCountInBuildSettings; i++)
    2.         {
    3.             Debug.Log(i);
    4.             var x = SceneUtility.GetScenePathByBuildIndex(i);
    5.             Debug.Log(x);
    6.         }
    7.  
     
    RenanRL, ErikH2000 and laurentlavigne like this.
  5. atomicjoe

    atomicjoe

    Joined:
    Apr 10, 2013
    Posts:
    1,866
    Old thread but as I was struggling with this myself, this was the first google result so lets get a definitive answer for future references!
    As suggested by @Lurking-Ninja , the solution is using:
    Code (CSharp):
    1. int buildIndex = SceneUtility.GetBuildIndexByScenePath("path or name of the scene");
    If the scene doesn't exist (or wasn't included in the build) the buildIndex value will be -1 , so you can check this out before actually trying to load the scene and cancel if the build index is less than zero :)
     
    Starbox, Fbrzd, Karinnn and 8 others like this.
  6. swingingtom

    swingingtom

    Joined:
    Feb 15, 2018
    Posts:
    9
    Following @atomicjoe move
    And if we need an editor only answer :

    Code (CSharp):
    1.  
    2. using UnityEditor;
    3.  
    4. public static bool IsSceneNameInProject(string named)
    5. {
    6.     foreach (EditorBuildSettingsScene scene in EditorBuildSettings.scenes) {
    7.         if (scene.enabled) {
    8.             if (scene.path.Contains("/" + named + ".unity")) {
    9.                 return true;
    10.             }
    11.         }
    12.     }
    13.     return false;
    14. }
    Or with linq if we already use it
    Code (CSharp):
    1.  
    2. using System.Linq;
    3. using UnityEditor;
    4.  
    5. public static bool IsSceneInProject(string named)
    6. {
    7.        
    8.     return EditorBuildSettings.scenes.Any(scene => scene.enabled && scene.path.Contains("/" + named + ".unity"));
    9.  
    10. }
     
    Starbox and atomicjoe like this.
  7. Anvarito

    Anvarito

    Joined:
    Sep 13, 2017
    Posts:
    7
    And how do I find out that the scene has already been loaded from the Asset Bundle, and that it is already valid and not to download it a second time?
    it's just that this method did not work in my case, after loading the scene from the bundle, I always get -1 in her name
     
  8. atomicjoe

    atomicjoe

    Joined:
    Apr 10, 2013
    Posts:
    1,866
    I guess this method is only valid for scenes included in the build process, not the ones loaded from Asset Bundles.
    But since Asset Bundles are loaded manually, you can keep track of it manually: as soon as the Asset Bundle is correctly loaded, you can expect the scene to be loaded too.