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?
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");
}
}
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. 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?
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");
}
}
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.
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 “”
}
}