- Home /
If Application.LoadLevel fails, load mainMenu
So I have that code as a method, but apparently I can't use the final if statement in C# because it's a void method. Can anyone suggest a workaround? If you have questions about the code itself I'm on hand to comment. :)
The levels are named 5x5001, 5x5002 and etc. When a level is completed, right now it should just load the next level. Obviously in future I will polish this and make it load an animation and everything, make it all smooth, but for now just needs to load the next level. Which I think that code would do fine. But if it fails due to there not being another level, I want it to just return to main.
void Update()
{
Vector3 pos = puck.transform.position;
string level;
int nextLevel;
if (pos.x == objectiveLoc.x && pos.z == objectiveLoc.z)
{
level = Application.loadedLevelName;
nextLevel = int.Parse(level.Substring(level.Length - 3));
if(!Application.LoadLevel("5x5" + nextLevel))
Application.LoadLevel("mainmenu");
}
}
Okay so I rewrote that method cos it was wrong anyway and would just load the same level, so here's the new one:
void Update()
{
Vector3 pos = puck.transform.position;
int nextLevel=0;
if (pos.x == objectiveLoc.x && pos.z == objectiveLoc.z)
{
try
{
nextLevel = int.Parse(Application.loadedLevelName.Substring(Application.loadedLevelName.Length - 3));
nextLevel++;
Application.LoadLevel("5x5" + nextLevel.ToString ("D3"));
}
catch(System.Exception e)
{
Debug.Log (e);
Application.LoadLevel("mainmenu");
}
}
}
However, this still won't load the mainmenu scene. So if anyone could help me out with that, that'd be awesome.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Initialising List array for use in a custom Editor 1 Answer
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer
C# if-statement optimization 1 Answer