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");
}
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");
}
Use Application.loadedLevel or Application.loadedLevelName to check the current level that is being played.
If(Application.loadedLevelName== “levelxname”)
Application.LoadLevel(“levelxname”);
// or
If(Application.loadedLevel == 1)
Application.LoadLevel(2);