Issue using .Contains with application.loadedlevel/loadlevel

I’m working on a script that returns to the correct scene when pressing escape. In the DataUpgrade, Level Select, TransferData(x) it should return to StartMenu, in StartMenu it should close the game and in a Level(x) it should return to TransferData(x).

#pragma strict
import System;

function Update () {
    if (Input.GetKey ("escape")) 
        var level : String = Application.loadedLevel.ToString();

        //var transferData = "TransferData";

        if(level == "StartMenu"){

        else if(level == "DataUpgrade"){

        else if(level == "Level Select"){

        else if(level.Contains("TransferData"))

        else if(level.Contains("Level"))
            Application.LoadLevel (Application.loadedLevel - 1);

        //trying this out too but isn't working either
        /*else if( transferData in level)

        else if("Level" in level)
            Application.LoadLevel (Application.loadedLevel - 1);*/  

i’m not sure why it’s not working, the scenes have the correct names and have also been added to build settings. Thanks in advance

Application.loadedLevel is an integer. Specifically, it’s the index of your scene in the build settings.

Which means that Application.loadedLevel.ToString() is just that integer as a string. Try to put a Debug.Log(level) after you assign level - you’re going to get something like “0” or “1”.

To get the name of the level, use Application.loadedLevelName:

var level = Application.loadedLevelName;