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

Question Is this the best way to avoid hardcoding scene names?

Discussion in 'Scripting' started by ShiftyGames, Apr 25, 2021.

  1. ShiftyGames

    ShiftyGames

    Joined:
    Sep 10, 2018
    Posts:
    16
    I'm aware that I can load scenes by using their name.
    Code (CSharp):
    1. SceneManager.LoadScene("Hellpit");
    Obviously hardcoding strings everywhere is a problem as the I could later decide to change the name of the scene. My solution would be to create a class storing all the scene names in one place so that they're easier to alter.
    Code (CSharp):
    1. public class SceneName
    2. {
    3.     public const string hellpit = "Hellpit";
    4. }
    Then
    Code (CSharp):
    1. SceneManager.LoadScene(SceneName.hellpit);
    Is this the best way of doing this?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Dunno if it's the best way, but that's how I do it!

    Your instinct to avoid splattering string literals all over your project is a Very Good Thing(tm).

    ANOTHER even more-centralized way is to make your SceneName class actually contain functions that take you to places in the game. If you put 100% of your scene loads inside each function, then if suddenly you need to additively load multiple scenes (also a GREAT way to work!), you know you only have to change it in that function and you're done.

    Here is my blurbery on additive scenes:

    Additive scene loading is one possible solution:

    https://forum.unity.com/threads/right-way-for-performance-divide-scene.1023673/#post-6630961
    https://forum.unity.com/threads/right-way-for-performance-divide-scene.1023673/#post-6754330

    https://forum.unity.com/threads/problem-with-canvas-ui-prefabs.1039075/#post-6726169

    A multi-scene loader thingy:

    https://pastebin.com/Vecczt5Q

    My typical Scene Loader:

    https://gist.github.com/kurtdekker/862da3bc22ee13aff61a7606ece6fdd3

    Other notes on additive scene loading:

    https://forum.unity.com/threads/removing-duplicates-on-load-scene.956568/#post-6233406

    Timing of scene loading:

    https://forum.unity.com/threads/fun...ject-in-the-second-scene.993141/#post-6449718
     
    ShiftyGames likes this.
  3. ShiftyGames

    ShiftyGames

    Joined:
    Sep 10, 2018
    Posts:
    16
  4. MDADigital

    MDADigital

    Joined:
    Apr 18, 2020
    Posts:
    2,198
    You can often make it even more data driven than that though so you have some state machine handling it so you never hardcode

    Code (CSharp):
    1. SceneManager.LoadScene(SceneName.hellpit)
    But it depends on the use case and domain. We are a MP game and our scenes are configured using a scriptable object were we keep more things like game mode setup etc
     
  5. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    Maybe use enums?
    Code (CSharp):
    1.   public enum Scenes
    2.     {
    3.         Scene1, Scene2, Scene3
    4.     }
    5.  
    6. SceneManager.LoadScene(Scenes.Scene1.ToString());