I have some strings in a class that should be the same name as the many scenes in my application.
I want to ensure that when I call SceneManager.LoadScene with the string that I do not get an error saying the scene couldn’t be loaded because it has not been added to the build settings. This needs to be checked perhaps on start of the application running so that further down the line I don’t get an error due to there being a typo in the string, or the scene name being changed only in the Unity project and not in the string.
There may be a much better way to do this than I have currently implemented, so if there is please tell me!
2 Answers
2
If you have a class that handles listing out the scene names, why not add a public int to that, that also gets that scene ID, and load that instead of the name? Or use .Contains.toLower instead, so you dont have to worry about it being EXACT, just close enough.
Im not exactly sure what your class is called or how your accessing it, so I cant provide a code example, but essentially, youd be looking at an if statement, something like (pseudo code):
if ( yourClass.sceneName.ToLower().Contains("some scene name")){ SceneManager.LoadScene(yourClass.sceneName); }
And if you go with the int ID idea:
SceneManager.LoadScene(yourClass.sceneID);
But thats assuming your actual class file is referenced as a “include” in whatever script will be running these if-statements/loading SceneManager, and that in that class, “sceneName” is a public string, and “sceneID” is a public int, that youd set at runtime technically.
Unfortunately, it doesn’t work to read out the scenes in the build settings via the new SceneManager, at least not at the moment (not sure if that’s intention or a silly bug).
Therefore, you can at least use some workarounds with UnityEditor namespace:
-
UnityEditor.EditorBuildSettings.scenes returns all the scenes which are currently added to the build settings in the editor.
-
Downside: You are only able to get a scene’s path this way, so you have to parse it in order to get the actual scene name.
But if you parsed those and extract the names, you can check if
-
you’ve got scene names in your list which are not registered in the buildsettings and
-
you have scenes in the build settings which do not match any of your defined strings
and sort out any problems manually.
If you had a scene list which does not change anymore, this could also be solved pretty easily with an editor script to automatically update the scene names. But as you suggested, the list may change so the assignment of the scene names to the proper variables will no longer be that trivial.
There have also been approaches to create an enum, for which I had written a quick test script a few days ago. With that approach you could read the scene names and always update your C# enumeration. The enumValue.ToString() would return the scenename.
If you use one of the enum values while the corresponding scene changes its name or is removed from the list, you’ll get a compiler error so that you have to make sure you fix it.
Aah i'm not getting the particles every frame right now, I'm just setting them. Going to try out both get and set every frame if it helps :)
– PiilorasvaI tried it and it worked, awesome! Thank you so much! :) I will post this as an answer to this question.
– Piilorasva