Loading Next Scene
Hello I am having trouble to load next level/scene using a script that check if player had collided a cube. I have twenty different scene naming map1, map2, map3 and so on.
I can't find and write a script foe that, can anyone please help? thanks
Answer by witylernn · Oct 20, 2017 at 02:49 AM
This is an okay option, just repetitive
string currentScene;
private void Update()
{
if (collidedWithCube)
{
currentScene = SceneManager.GetActiveScene().name;
switch (currentScene)
{
case "map1":
SceneManager.LoadScene("map2");
break;
case "map2":
SceneManager.LoadScene("map3");
break;
//Continue on like this for all of your levels.
}
}
}
This is a better option of you stick to the map1, map2, map3 naming convention,
string currentScene;
string spliced;
int levelNumber;
private void Update()
{
if (collidedWithCube)
{
currentScene = SceneManager.GetActiveScene().name;
spliced = currentScene.Substring(currentScene.Length - 1, 1); // this gets you the last character of the scene name i.e "1", "2", etc
levelNumber = int.Parse(spliced) + 1;
SceneManager.LoadScene("map" + levelNumber.ToString());
}
}
So do me a favor and write 5 lines of code that also grabs the second to last character in the string, check if its a number, if it is, then use it.
Thanks, buddy.
How about 3 lines ?
string sceneName = Scene$$anonymous$$anager.GetActiveScene().name ;
int levelNumber = int.Parse(System.Text.RegularExpressions.Regex.$$anonymous$$atch(sceneName, @"map(\d+)" ).Groups[1].Value) + 1;
Scene$$anonymous$$anager.LoadScene("map" + levelNumber.ToString());
Or even 1 :
Scene$$anonymous$$anager.LoadScene("map" + (int.Parse(System.Text.RegularExpressions.Regex.$$anonymous$$atch(Scene$$anonymous$$anager.GetActiveScene().name, @"map(\d+)" ).Groups[1].Value) + 1).ToString());
Answer by Owen-Rowley · Oct 22, 2017 at 10:20 PM
Okay, sorry guys; I've tried put that in cube but it doesn't work...
I guess something like that cube detect if player (with the tag "Player") had collided that cube itself, these script should work but didn't??
remember i am very novice so may i ask you to put whole script ready so i can copy and paste? I know i sound very lazy but for good reason tho,m i always screwed up if i tried it myself...
Anyway thanks!
Your answer
Follow this Question
Related Questions
Teleporting problems 1 Answer
How can I handle scripts correctly? What's a good way to apply scripts to GameObjects? 1 Answer
[Question] Unity C# Set a Player Class and refence issue 0 Answers
Reload Current Scene with Scene-manager 3 Answers
how can i save highest played level?,how can i save number of highest played level? 0 Answers