OnCollisionEnter.. Load new scene.

Hi,

so when my ‘Man’ character’s collider, collides with my ‘house’ collider, I effectively want to load a new scene named ‘indoor’

I currently have this script, it compiles without errors however on collision of the two mentioned GameObjects… the scene does not change. Any explanation for this?

Here is my script:

using UnityEngine;
using System.Collections;

public class New_Level : MonoBehaviour {

void OnCollisonEnter(Collider Man)
{
if (Man.gameObject.tag == “house”)
Application.LoadLevel (“indoor”);
}

}

Thanks.

Inside the brackets for your load level it needs to be an int that is the scene number. The scene number is the same as the one in your build settings

1 Like

Since this is the 2D forum, I have to ask if you are sure you want to use OnCollisionEnter. That is for the 3D physics.

Application.LoadLevel, can take the level name as a string, so that isn’t your problem. However, OnCollisionEnter does not take a Collider as input, but rather a Collision. So try this:

using UnityEngine;
using System.Collections;

public class New_Level : MonoBehaviour
{
    void OnCollisionEnter(Collision Man)
    {
        if (Man.gameObject.tag == "house")
            Application.LoadLevel("indoor");
    }
}
1 Like

Ahh thank you for the help, in my original code I did have Collision and stupidly copied the code I was editing (messing around with) to get this to work to the thread.

Anyway, I still copied your script word for word and still I get no response. :frowning: Is there something you can suggest other than OnCollisionEnter or have I set up my GameObjects wrong… both have colliders enabled, and one does have a rigidbody?

Thank you for the help.

I did a test of the script and it worked in my scene. I had two objects, one with a RigidBody (not RigidBody2D) and both with Sphere Colliders.

You’re using the correct RigidBody? And it’s the correct tag? And they colliders aren’t set to trigger?

1 Like

I’m working only in 2D and therefore I can only have Rigidbody 2D assigned. I assume OnCollisionEnter is the problem then?

Colliders aren’t set to trigger, and I’m confused as to what is the correct tag? Sorry :frowning:

OK, then the problem is that you’re using OnCollision, which is for the 3D physics. Change your code like this:

using UnityEngine;
using System.Collections;
public class New_Level : MonoBehaviour
{
    void OnCollisionEnter2D(Collision2D Man)
    {
        if (Man.gameObject.tag == "house")
            Application.LoadLevel("indoor");
    }
}
1 Like

Still… nothing… :eyes: I clearly have something set-up wrong on my end… Thank you for the assistance anyway, appreciate it.

What do you have the script on?

1 Like

The ‘Man’ GameObject, who also has RIgidBody2D assigned to it as well as a Box collider 2D.

Put a break point in & debug or put a debug line in to print to the console so you can check if the collision is registering.

1 Like

Yep, the console detected the collision.

Okay I got it working with this…

{
void OnCollisionEnter2D(Collision2D Man)
{
if (Man.gameObject.tag == “house”);
Application.LoadLevel(“indoor”);
}

A semi colon needed to be presented on both lines… my bad guys, Thank you for the help.

No, your code still isn’t working. Having a semicolon at the end of an if, means that it won’t do anything and the line with Application will be executed no matter which tag the colliding object has.

But, this means that there is something wrong with your tag check. Is the object really tagged correctly? Remember that upper/lower case matter.

1 Like

Thanks a lot, the ‘house’ wasn’t tagged correctly (not tagged at all :/). Works correctly now, thanks once again.

I used a simple script that I made. It works for both 2D and 3D games as long as you edit the code for each type of game. Make sure that the scenes are in the build settings, your tags are correct, names of the player is correct, and attach the script to the object that loads a new scene and that it’s a trigger. This is the script that loads a new scene on trigger.

using UnityEngine;
using System.Collections;

public class LoadNewScene : MonoBehaviour
{

public void NewLebel() // This MUST be inserted otherwise the script doesn’t work, also the name of the scene to be loaded has to be where it says NewLebel (delete NewLebel and replace it with your scene name
{
Debug.Log(“NewLebel”);
Application.LoadLevel(“NewLebel”);
}

void OnTriggerEnter(Collider co)
{

//this finds the game object called “Ball” in the scene
if (co.name == “Ball”) // If what collided with the cube is called “Ball” it will do what the code says which is load a new scene. If your player is called “Hulk” in the scene you would put “Hulk” instead of ball
Debug.Log(“NewLebel”);
Application.LoadLevel(“NewLebel”); // this looks for the scene that is call “NewLebel” and loads it. Whatever scene you want to load you put in the “”, say your scene is called “Goat” you put that in the “”
}
}

I found this code on internet, maybe it would help …

  • function OnCollisionEnter(other : Collision){
  • if(other.gameObject.name == “SomeObject”){
  • Application.loadlevel(“Level name”);
  • }
  • if(other.gameObject.tag == “SomeTag”){
  • Application.loadlevel(“Level name”);
  • }
  • }

just replace coliders with 2dcolliders thingi …(OnCollisionEnter => OnCollisionEnter2D)

yup…also a beginner…