- Home /
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"){
Application.Quit();
}
else if(level == "DataUpgrade"){
Application.LoadLevel("StartMenu");
}
else if(level == "Level Select"){
Application.LoadLevel("StartMenu");
}
else if(level.Contains("TransferData"))
Application.LoadLevel("StartMenu");
else if(level.Contains("Level"))
Application.LoadLevel (Application.loadedLevel - 1);
//trying this out too but isn't working either
/*else if( transferData in level)
Application.LoadLevel("StartMenu");
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
Answer by Baste · Nov 13, 2014 at 01:09 PM
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;
Thanks man this solved the issue and i now i also know why :) there's one thing i don't understand tho: the first 3 blocks of code(with DataUpgrade, Level Select, Start$$anonymous$$enu) did work, it was only the .Contains not working but with your explanation those blocks also shouldn't have worked right?
Your answer
Follow this Question
Related Questions
Create reset button to main camera 1 Answer
Animation not being played! 0 Answers
Use c++ Code in Unity3D 0 Answers
How to create a UnityScript array and access the data in each cell. 1 Answer
How to handle 5 touches at a time? 2 Answers