- Home /
Change level script
I have a script I used all the time in Unity 4 and now I think Unity 5 has changed some stuff to where the script wont work. I am also gonna ask is there a way to where I can set it to where all I have to do is put the script on a trigger then the scene so I don't need to make so many scripts?
Script: C#
using UnityEngine;
using System.Collections;
public class L12L2 : MonoBehaviour {
void changesceneto() {
Application.LoadLevel (2);
}
}
Answer by hnmikechan · Jun 27, 2015 at 01:55 AM
You're command is correct, you just need to make sure all your scenes are setup correctly in the build settings: Application.LoadLevel(2);
You don't need to make a new script for each Application.LoadLevel();
in a single script you could have multiple methods loading different levels;
public void goToSceneOne(){
Application.LoadLevel(1);
}
public void goToSceneTwo(){
Application.LoadLevel(2);
}
or else you could pass an int value:
public void switchScene(int i){
Application.LoadLevel(i);
}
I am seriously dumb, i forgot to add the scene to the build. I am face pal$$anonymous$$g so hard right now here is a picture of how hard.
Should probably accept one of the answers that recommended this fix ;)
Answer by lloladin · Jun 23, 2015 at 11:38 AM
youre load lvl seems to be correct did you remember to add the scene to youre build? and to avoid making so many scripts you could make
Public String LevelToLoad = "Level1";
and then use the string inside youre
Application.LoadLevel (LevelToLoad);
just change the Strings text per scene
I've ended up adding that and it says namespaces could only contain types and namespace declarations
here it is using UnityEngine; using System.Collections;
public string levelToLoad = "Level1";
public class LevelChange : $$anonymous$$onoBehaviour {
void update() {
Application.LoadLevel (levelToLoad);
}
}
First of all, you are trying to call Application.LoadLevel (levelToLoad); in the Update function = very, very, very bad idea
Put public string levelToLoad = "Level1"; inside your class
Your answer
Follow this Question
Related Questions
Convert line of Csharp to Javascript 1 Answer
I cannot figure out what my error is... 2 Answers
Working with interfaces, best practices? 1 Answer
Button not executing function. C# 3 Answers