Search Unity

Bug Weird behaviour on SceneManagement.LoadScene

Discussion in 'Scripting' started by coder091, Jun 19, 2021.

  1. coder091

    coder091

    Joined:
    Feb 16, 2021
    Posts:
    23


    ^ So this is my scenes in build.

    First it loads my mainScene automaticly. When I press the start button to load onto next level something weird happens.

    All the parameteres that i'm passing into my level manager function is correct. Like which level to load etc.




    levelToLoad is 1 (passed from saved data/first login)
    getscenebybuildindex(leveltoload) returns a "Scene"
    when i try to get the .name of it, it returns null.

    then when i try to load the scene with SceneManager.GetSceneByBuildIndex(levelToLoad).buildIndex

    it returns ; Cannot load scene: Invalid scene name (empty string) and invalid build index -1


    could this be a bug?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,911
    Not a bug. Just a misunderstanding of the SceneManagement API on your part.

    https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.GetSceneByBuildIndex.html
    You can fix this by removing the unecessary GetSceneByBuildIndex call from your code:
    Code (CSharp):
    1. SceneManager.LoadSceneAsync(levelToLoad, LoadSceneMode.Additive);
     
    coder091 likes this.
  3. coder091

    coder091

    Joined:
    Feb 16, 2021
    Posts:
    23
    Honestly I did that before and it worked but it didn't feel right somehow. Hard coding an integer on a scene loader didnt seem right to me.

    Thanks for the clarification.
     
  4. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,998
    Yeah, I had the same issue. I'm using scenes in a methodical manner and am happy to name and load them using A1, A2, A3... and S1, S2, S3... but by-name can't be done in Unity, only by number. Since they're still being made I've got the A's in some weird oder like 2, 4 and 9 and the S's as maybe 3, 6, 7, 11... . Looks really ugly.

    What I finally did is make a single table that looked like: A:2,4,9. S:3,6,7,11. Now my own AddScene("A2") function can look them up using that table. Whenever I rearrange the scene order I still have to redo the table to match, but it's not as bad as screwing with constants in the code.
     
  5. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,203
    On the contrary you can refer to them by name just fine. Limitations are described in the following document pages but the short version is if you want to refer to them by just the name they need to be in the build list otherwise you have to refer to them by their complete path.

    https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html
    https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadSceneAsync.html

    For projects with many scenes you can automate the process of adding them to the build list.

    https://docs.unity3d.com/ScriptReference/EditorBuildSettings-scenes.html

    Example below searches "Assets/Scenes" and all its subfolders generating a new list from all the scenes it finds.
    Code (csharp):
    1. using System.Collections.Generic;
    2. using System.Linq;
    3. using UnityEditor;
    4.  
    5. public class BuildListExample
    6. {
    7.     [MenuItem("Tools//Populate Build List")]
    8.     static void PopulateBuildList()
    9.     {
    10.         var scenes = new List<EditorBuildSettingsScene>();
    11.  
    12.         foreach (var path in AssetDatabase.FindAssets("t:Scene", new string[] { "Assets//Scenes" }).Select(AssetDatabase.GUIDToAssetPath))
    13.         {
    14.             scenes.Add(new EditorBuildSettingsScene(path, true));
    15.         }
    16.  
    17.         EditorBuildSettings.scenes = scenes.ToArray();
    18.     }
    19. }
     
    Last edited: Jun 19, 2021
  6. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,998
    Ha! I started with "load a scene by number" and thought, "OK, I just need to translate my scene name into a number, and there's GetSceneByName...", but it doesn't do that! (it only looks at loaded scenes, which is sort of the same problem the OP had). After 20 minutes I decided Unity can't translate a scene name into it's # in the build list. I completely forgot to check if loading by name could be done directly. And for anyone who knows Unity, we know it works like that all the time: we need X to do Y, and there's no visible way of doing X, but that's because Unity didn't expose X. Unity has a command for Y using it's internal X.