- Home /
How do I load a new scene based on current scene?
I want to use the same "DoorEnter.cs" script for every level in my game. Is there a way I can declare "if scene = scene0, LoadScene1();" ?
Here is my sample code. The commented out code is what I'm trying to figure out here:
public void OnTriggerStay2D (Collider2D other) {
if(other.gameObject.name == "Player") {
Debug.Log ("DoorEnter is triggered");
if(Input.GetKey(KeyCode.F)) {
//if scene = scene 0, LoadScene1();
//if scene = scene 1. LoadScene2();
}
}
}
void LoadScene1 () {
Application.LoadLevel ("scene1");
}
void LoadScene2 () {
Application.LoadLevel ("scene2");
}
I feel like a fool. I tried loadedLevelName earlier but just had a syntax error, appearentyl! Thank you for the comment and help, cheers!
@Harshad$$anonymous$$, Sorry man, i tried to move your comment to an answer, but i think you set it not to everyone and is now lost in UA ether, very sorry.
@moses No issues. It happens. In the end, you were trying to help me. And that's what matters, helping each other. :-)
Answer by Stealthygolem · Oct 09, 2014 at 12:37 PM
Can you answer your own question? Maybe people will find it useful in the future:
public void OnTriggerStay2D (Collider2D other) {
if(other.gameObject.name == "Player") {
Debug.Log ("DoorEnter is triggered");
if(Input.GetKey(KeyCode.F)) {
if(Application.loadedLevelName == "scene0") {
LoadScene1 ();
}
if(Application.loadedLevelName == "scene1") {
LoadScene2 ();
}
}
}
}
void LoadScene1 () {
Application.LoadLevel ("scene1");
}
void LoadScene2 () {
Application.LoadLevel ("scene2");
}
Yes, you can answer your own question ;)
Just for the record, you can also load a level by the level index. So to simply load the next level in order, just call:
if (Application.loadedLevel + 1 < Application.levelCount)
Application.LoadLevel( Application.loadedLevel + 1 );
However in more complex games where you might want to go from level 3 to level 7 it's easier to use a public variable with a the level name / index the script should load. This variable can be set in the inspector.
With the caveat of no points for self service, but the honor of helping the community.
Answer by Redeemer86 · Oct 09, 2014 at 12:37 PM
If(Application.loadedLevelName== "levelxname")
Application.LoadLevel("levelxname");
// or
If(Application.loadedLevel == 1)
Application.LoadLevel(2);
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Health script, save health in between scenes. PlayerPrefs. 2 Answers
Distribute terrain in zones 3 Answers
Calling an Audio Source on one game object from a script on another game object..? 1 Answer
I want to set play scene of network lobby manger through script 0 Answers