Load next level script?

I’m working on a simple platforming game, and I have it trigger the next level when your character hits the final platform. I was wondering if there is some way to make it so instead of using Application.LoadLevel (2) or whatever level it might be I could just make it load the next one? It would save me a lot of time because I could just make a single prefab platform with the script instead of having to go in and edit the script for every level.

All help is appreciated…

Erm… this is far easier.

Make a prefab of your platform attach the load level script. In the code, put this:

var LevelToLoad=0;
function OnTriggerEnter (other : Collider) {
Application.LoadLevel (LevelToLoad);
}

Then simply change the number in the inspector.

1 Like
Application.LoadLevel(Application.loadedLevel+1);

Is this what you’re looking for?

1 Like

Yes but I can’t seem to get it to work… this is the entire script is it correct?:

function Update () {
}

function OnTriggerEnter (other : Collider) {
	Application.LoadLevel (Application.loadedLevel+1);
}

It just keeps on loading my level 0

1 Like

maybe try

 var levelToLoad : int;

function Awake () {
        levelToLoad = Application.loadedLevel + 1;
}

Look in your build settings and make sure that you have included more than one level.

Also, in the Start function on this object… add something that tells you the level count. :wink:

Yes, make sure you have more than one level in your build settings.

I just tried the code, and it works perfectly.

You were completely right, I was testing on level 16 and I’ve only added like the first 10 levels to the build settings.

It works now so I’m all good.

Thanks for the help everyone :wink:

Finally after 4 hours of wasted time…I tried using this and it worked! Yes, you have to create another level and build it. FILE / BUILD SETTINGS etc. This works!!! change the last line to this:

Application.LoadLevel (Application.loadedLevel+1);

sample script below:

using UnityEngine;
using System.Collections;

public class Remover : MonoBehaviour
{
public GameObject splash;

void OnTriggerEnter2D(Collider2D col)
{
// If the player hits the trigger…
if(col.gameObject.tag == “Player”)
{
// … stop the camera tracking the player
GameObject.FindGameObjectWithTag(“MainCamera”).GetComponent().enabled = false;

// … stop the Health Bar following the player
if(GameObject.FindGameObjectWithTag(“HealthBar”).activeSelf)
{
GameObject.FindGameObjectWithTag(“HealthBar”).SetActive(false);
}

// … instantiate the splash where the player falls in.
Instantiate(splash, col.transform.position, transform.rotation);
// … destroy the player.
Destroy (col.gameObject);
// … reload the level.
StartCoroutine(“ReloadGame”);
}
else
{
// … instantiate the splash where the enemy falls in.
Instantiate(splash, col.transform.position, transform.rotation);

// Destroy the enemy.
Destroy (col.gameObject);
}
}

IEnumerator ReloadGame()
{
// … pause briefly
yield return new WaitForSeconds(2);
// … and then reload the level.
Application.LoadLevel (Application.loadedLevel);

}
}