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

What the hell is the point of SceneManager.GetSceneAt()?

Discussion in 'Scripting' started by Bazz_boyy, Nov 8, 2016.

  1. Bazz_boyy

    Bazz_boyy

    Joined:
    May 22, 2013
    Posts:
    192
    This function doesn't even work. It's required that a scene has to have previously been loaded in game... Why?

    I can start a scene by using SceneManager.LoadScene(int index) but I can't use SceneManager.GetSceneAt(int index)?

    How the hell does this make any sense??

    #mad
     
    Tonymotion likes this.
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    I guess you missed the bit about the new scenemanager were you can have multiple scenes loaded and "live" at the same time?
     
  3. Bazz_boyy

    Bazz_boyy

    Joined:
    May 22, 2013
    Posts:
    192
    Well my game has like 20+ levels... are you suggesting I load all my levels at the start of my application? Is the SceneManager going to handle the memory somehow? Why would I load scenes the player might not even access or have access to? Seems inefficient and cumbersome.

    Sorry to ask u so much but couldn't find any helpful resources regarding the SceneManager.
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,181
    The docs for SceneManager are really bad!

    GetSceneAt is for selecting one of your already loaded scenes. The index is the index at which they're loaded, not their index in the build settings.

    I guess it could be used to iterate the currently loaded scenes? But if you have a game where you load a bunch of scenes simultaneously, you should probably have some kind of script managing the loaded scenes, and not get them by an index.

    By the way, do you have an actual problem you need help with, or did you just need to rant?
     
    Tonymotion, a436t4ataf and jhirsh like this.
  5. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    Quite simple. A scene is not necessarily a level, but can be multiple things. Take for example a game (or level) which plays in a house. In that game (or level) the hallways and each room might be separate scenes where you transition seamlessly in. Advantage of that is that you can save heavily on memory usage, as you don't need to load the entire environment in one go.
     
  6. Bazz_boyy

    Bazz_boyy

    Joined:
    May 22, 2013
    Posts:
    192
    Okay I see.

    Yes I do actually have a problem lol. I'm trying to store a dictionary of <LevelData, string>, the string being the level scene's name and the LevelData being some data from the level like high scores, whether it has been beaten or not, etc. I want my script to be able to add to the dictionary dynamically if new levels are added, but I can't get the scene names from the SceneManager. I need only the levels specifically and not other scenes obviously...

    Here's what I tried:

    Code (CSharp):
    1.      
    2.         int sceneCount = SceneManager.sceneCountInBuildSettings;
    3.         for (int i = 0; i < sceneCount; i++)
    4.         {
    5.             string sceneName = SceneManager.GetSceneAt(i).name;
    6.             if(sceneName.Contains("Level"))
    7.             {
    8.                 levelDataHolder.Add(sceneName, new LevelData());
    9.             }
    10.         }
    Obviously this didn't work, so here I am complaining.

    Edit: GetAllScenes() looks nice but it's deprecated lol.
     
    Last edited: Nov 8, 2016
    GDesmoulins likes this.
  7. Bazz_boyy

    Bazz_boyy

    Joined:
    May 22, 2013
    Posts:
    192
    Oh okay I see... That is pretty cool.
     
  8. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,181
    Yeah, it's because you're mixing two concepts:
    - the scenes that exist in your game.
    - the scenes that are currently loaded.

    A problem with the design is that you can't get a list of the scenes that exist, but are not loaded at runtime. That's only available at editor time, through EditorSceneManager.


    The best solution for you is probably to make levelDataHolder be lazy. So if you ask for the data for a scene, but that scene's LevelData hasn't been added, just add it then.
     
    Tonymotion likes this.
  9. lrb

    lrb

    Joined:
    Jun 21, 2014
    Posts:
    27
    I just give up and used the legacy one:

    Code (CSharp):
    1.         var currentScene = EditorApplication.currentScene;
    2.         foreach (var scene in EditorBuildSettings.scenes)
    3.         {
    4.             EditorApplication.OpenScene(scene.path);
    5.             //Do some stuff
    6.             EditorApplication.SaveScene();
    7.         }
    8.  
    9.         EditorApplication.OpenScene(currentScene);
    I really can't do this with new engine.