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

Resolved How I can translate scene index to a different scene

Discussion in 'Scripting' started by pareylook, Jun 12, 2020.

  1. pareylook

    pareylook

    Joined:
    Mar 4, 2016
    Posts:
    10
    Hello all!
    Maybe my question is stupid but I can't find information about this.

    I have a problem with my small game. In my game, I have some levels(Scenes) and if Player losing some level, load a scene(RestartScene) which contains a menu with a resume button. My plan Player clicks on button and load level which he lose. But I don't understand how I can translate lose level index to "RestartScene".
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,913
    Store the current level somewhere.

     
    pareylook likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    @pareylook I would recommend getting away from using the scene index number, and here's why:

    if anyone, you or any other team member you might work with, inserts or removes, or moved around a scene in the future, all scenes below that change their index, and there is no trivial easy way to even detect it happened.

    Just use the string. It's 2020 already... :)

    Personally I have a static class full of const strings:

    Code (csharp):
    1. public static class SceneNames
    2. {
    3.   public const string s_MainMenu = "mainmenu";
    4.   public const string s_Options = "options";
    5. }
    That keeps me from typing something like
    mainmemu
    and wondering why my game fails.
     
    pareylook likes this.
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Really makes you wonder why Scenes aren't able to be referenced seamlessly like other Unity objects.
     
    Kurt-Dekker likes this.
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    This ^ ^ ^ They share so much in common with Prefabs.

    Loading a scene additively is like loading a prefab and having to wait 1 frame to use it.

    Partial scenes additively loaded are like the best thing EVER...
     
    PraetorBlue likes this.
  6. pareylook

    pareylook

    Joined:
    Mar 4, 2016
    Posts:
    10
    Kurt-Dekker likes this.