Next Level Code Help

I’m trying to make a script to allow my player to move onto the next level when it collides with an object but I keep getting errors.

Assets/NextLevel.cs(14,9): error CS0103: The name `OnCollisionEnter’ does not exist in the current context

Assets/NextLevel.cs(16,23): error CS0103: The name `Level2’ does not exist in the current context

Assets/NextLevel.cs(16,13): error CS1502: The best overloaded method match for `UnityEngine.Application.LoadLevel(int)’ has some invalid arguments

Assets/NextLevel.cs(16,13): error CS1503: Argument #1' cannot convert object’ expression to type `int’

This is my code:

using UnityEngine;
using System.Collections;

public class NextLevel : MonoBehaviour {

    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () {
   
    OnCollisionEnter();

Application.LoadLevel(Level2);
   
    }
}

If someone could fix the code and tell me what was wrong that would be greatly appreciated.

You should probably start here.

Yeah… everything you’re doing is wrong :slight_smile:

If you click on some of the highlighted keywords in your code section (i.e. here in your forum post), it will take you directly to the help for those functions - very handy. e.g. click on OnCollisionEnter to see code samples of how that should be used.

The quick version though - you can pretty much delete your Start() and Update() functions (you don’t need them for what you’re doing). Then create an OnCollisionEnter() function and do your level loading in there.

Something like this should get you started.

using UnityEngine;
using System.Collections;

public class NextLevel : MonoBehaviour {
 
    void OnCollisionEnter(Collision collision){
        Application.LoadLevel("Level2");
    }
}
1 Like

Minus that out-of-place semicolon :slight_smile:

1 Like

Fixed. Teach me for copy and pasting from the OP.

Yeah I’m new to unity only been using it for a couple of weeks I’ve got it working using the code that BoredMoron provided the only problem I’m having now is that when it loads the next level everything is dark like there is no light in the scene but there is.

I really got to find myself a new handle.

I heard that “BoredMemberOfTheChurchOfJesusChristOfLatter-daySaints” is free.

1 Like