if (SceneManager.GetActiveScene().name == zzz...

Hi all,
Currently I am teleporting my player from a scene to another depending on the current scene, using this (just posting part of the code) :

 public string[] levelNames = {"level_1", "level_2", "level_3", "level_4", "level_5", "level_6"};
  void Limbo()
    {
        SceneManager.LoadScene("limbo");
    }
 if (PlayerData.Lives <= 0)
if (SceneManager.GetActiveScene().name == levelNames[0])
{
  Limbo();
}
if (SceneManager.GetActiveScene().name == levelNames[1])
{
  Limbo();
}

And so on and so onā€¦for every single level (6 so far). Isnā€™t there a better way to do it? Using build Index is the same, and I canā€™t use || to actually make the code shorter.
It works as it should but it makes the code pretty big and chaotic.
Ty all in advance

1 Like

Hm. How many scene names would not load limbo? Would any scene that the player could lose lives on call limbo? Why canā€™t you use ā€˜||ā€™ ?

There are various types of ā€œlimboā€ā€¦limbo 1, limbo 2 etcā€¦first 3 levels would load limbo 1 if you get to 0 lives, the other 3 levels would load limbo 2 and so onā€¦(I have a void Limbo2() function too in the actual code, and I will have to make more).
I canā€™t use || because it says ā€œcannot apply the operator || to bool and string.ā€

Okay, the error then must have been how you typed it.
did you try to type something like : if(this_string == this_otherString1 || this_otherString2) ?
With string or build index, you could do:
if (index >= x && index <= y)
or
if(scene name == string_array[0] || scene_name == string_array[1]) etc.

Sorry I didnā€™t use code tags, hopefully that makes sense. :slight_smile:

If you only have 6 levels, I would not even worry about something more complex than that.

edit: you could do this, too. (not sure if this is truly useful)

string [] levelNames = { "level_1", "level_2", "level_3", "level_4", "level_5", "level_6" };
string [] levelToLoad = { "limbo",   "limbo",    "limbo",     "limbo2", "limbo2" , "limbo2" };

// check..
string lname = SceneManager.GetActiveScene().name;
for(int i = 0; i < levelNames.Length; ++i) {
    if(levelNames[i] == lname) {
         SceneManager.LoadScene(levelToLoad[i]);
         return;
       }
 }
2 Likes

Well 6 levels for now, I aim for 40+ :smile:
And yeah, it was a typing error!
I was writing
if (scene_name == string_array[0] || string_array[1])
instead of
if(scene_name == string_array[0] || scene_name == string_array[1]) etc.

Ty a lot :slight_smile:

No problem :slight_smile:

1 Like