# Load next level script? (436736)

**URL:** https://discussions.unity.com/t/load-next-level-script-436736/436736
**Category:** Scripting
**Tags:** Scripting
**Created:** [February 15, 2011, 10:01pm UTC](https://discussions.unity.com/t/load-next-level-script-436736/436736 "2011-02-15T22:01:27Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![T8TRG8TR](https://avatars.discourse-cdn.com/v4/letter/t/ba8739/32.png) [@T8TRG8TR](https://discussions.unity.com/u/T8TRG8TR)
#### Post date: [February 15, 2011, 10:01pm UTC](https://discussions.unity.com/t/load-next-level-script-436736/436736/1 "2011-02-15T22:01:27Z")

</div>

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…

---

<div class="post-metadata">

### Author: ![bigmisterb](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/bigmisterb/32/406611_2.png) [@bigmisterb](https://discussions.unity.com/u/bigmisterb)
#### Post date: [February 15, 2011, 10:03pm UTC](https://discussions.unity.com/t/load-next-level-script-436736/436736/2 "2011-02-15T22:03:44Z")

</div>

Erm… this is far easier.

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

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

```

Then simply change the number in the inspector.

---

<div class="post-metadata">

### Author: ![DanielQuick](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/danielquick/32/223057_2.png) [@DanielQuick](https://discussions.unity.com/u/DanielQuick)
#### Post date: [February 15, 2011, 10:04pm UTC](https://discussions.unity.com/t/load-next-level-script-436736/436736/3 "2011-02-15T22:04:04Z")

</div>

```auto
Application.LoadLevel(Application.loadedLevel+1);

```

Is this what you’re looking for?

---

<div class="post-metadata">

### Author: ![T8TRG8TR](https://avatars.discourse-cdn.com/v4/letter/t/ba8739/32.png) [@T8TRG8TR](https://discussions.unity.com/u/T8TRG8TR)
#### Post date: [February 15, 2011, 10:35pm UTC](https://discussions.unity.com/t/load-next-level-script-436736/436736/4 "2011-02-15T22:35:00Z")

</div>

> [@DanielQuick](#):
>
> ```auto
> Application.LoadLevel(Application.loadedLevel+1);
> 
> ```
> 
> Is this what you’re looking for?

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

```auto
function Update () {
}

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

```

It just keeps on loading my level 0

---

<div class="post-metadata">

### Author: ![Mike\_L](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/mike_l/32/444114_2.png) [@Mike\_L](https://discussions.unity.com/u/Mike_L)
#### Post date: [February 15, 2011, 10:37pm UTC](https://discussions.unity.com/t/load-next-level-script-436736/436736/5 "2011-02-15T22:37:56Z")

</div>

maybe try

```auto
 var levelToLoad : int;

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

```

---

<div class="post-metadata">

### Author: ![bigmisterb](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/bigmisterb/32/406611_2.png) [@bigmisterb](https://discussions.unity.com/u/bigmisterb)
#### Post date: [February 15, 2011, 10:44pm UTC](https://discussions.unity.com/t/load-next-level-script-436736/436736/6 "2011-02-15T22:44:27Z")

</div>

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. 😉

> **[Unity - Scripting API: Application.levelCount](https://docs.unity3d.com/ScriptReference/Application-levelCount.html)**

---

<div class="post-metadata">

### Author: ![DanielQuick](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/danielquick/32/223057_2.png) [@DanielQuick](https://discussions.unity.com/u/DanielQuick)
#### Post date: [February 15, 2011, 10:45pm UTC](https://discussions.unity.com/t/load-next-level-script-436736/436736/7 "2011-02-15T22:45:18Z")

</div>

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

I just tried the code, and it works perfectly.

---

<div class="post-metadata">

### Author: ![T8TRG8TR](https://avatars.discourse-cdn.com/v4/letter/t/ba8739/32.png) [@T8TRG8TR](https://discussions.unity.com/u/T8TRG8TR)
#### Post date: [February 15, 2011, 10:47pm UTC](https://discussions.unity.com/t/load-next-level-script-436736/436736/8 "2011-02-15T22:47:35Z")

</div>

> [@bigmisterb](#):
>
> 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. 😉
> 
> [http://unity3d.com/support/documentation/ScriptReference/Application-levelCount.html](http://unity3d.com/support/documentation/ScriptReference/Application-levelCount.html)

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 😉

---

<div class="post-metadata">

### Author: ![townsendmedia](https://avatars.discourse-cdn.com/v4/letter/t/54ee81/32.png) [@townsendmedia](https://discussions.unity.com/u/townsendmedia)
#### Post date: [April 21, 2014, 4:40am UTC](https://discussions.unity.com/t/load-next-level-script-436736/436736/9 "2014-04-21T04:40:00Z")

</div>

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);**

}  
}
