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

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

1 Like

I guess you missed the bit about the new scenemanager were you can have multiple scenes loaded and “live” at the same time?

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.

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?

3 Likes

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.

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:

        int sceneCount = SceneManager.sceneCountInBuildSettings;
        for (int i = 0; i < sceneCount; i++)
        {
            string sceneName = SceneManager.GetSceneAt(i).name;
            if(sceneName.Contains("Level"))
            {
                levelDataHolder.Add(sceneName, new LevelData());
            }
        }

Obviously this didn’t work, so here I am complaining.

Edit: GetAllScenes() looks nice but it’s deprecated lol.

1 Like

Oh okay I see… That is pretty cool.

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.

1 Like

I just give up and used the legacy one:

        var currentScene = EditorApplication.currentScene;
        foreach (var scene in EditorBuildSettings.scenes)
        {
            EditorApplication.OpenScene(scene.path);
            //Do some stuff
            EditorApplication.SaveScene();
        }

        EditorApplication.OpenScene(currentScene);

I really can’t do this with new engine.