Load Level based on Array ?

I wanted to load the next level based on array once entered a trigger. And so, i want each level have this script and load the next level of its own.

function OnTriggerEnter (other : Collider) {    
    if(other.gameObject.CompareTag("End")) {
        yield WaitForSeconds(1);
        Application.LoadLevel(1);
      }
}

This script doesn’t do as i wanted. In level 1, it load the level 2. But in level2, it load level2 again and not level3. So, you got what i mean? Then, how?

You don't need an array. Try this:


var next_level = 0;

function OnTriggerEnter (other : Collider) {    
    if(other.gameObject.CompareTag("End")) {
        yield WaitForSeconds(1);
        next_level=(Application.loadedLevel)+1;  //<- Here, you check the index of the actual level, and inrease it
        Application.LoadLevel(next_level);   //<- Here, you load the level with the new index
      }
}

Just add the test to handle the special case of the last level.