- Home /
Changing Scenes on 2D Collision
So I'm new to scripting and I have a 2D game. I want to change scenes when my player collides with another quad. They both have 2DBox Colliders, the quad's box collider is checked as a trigger, the player is tagged "Player" and this script is attached to the quad.
using UnityEngine;
using System.Collections;
public class CollisionSceneChange : MonoBehaviour
{
public string level = "testingscene2";
// Use this for initialization
void OnCollisionEnter2D (Collision2D Colider)
{
if(Colider.gameObject.tag == "Player");
Application.LoadLevel(level);
}
}
it gives me no syntax errors and the game runs but it doesn't work. I tried with OnTriggerEnter2D but that doesn't work either. Can somebody please help?
You have no idea what I've been fighting with this. Your code, with some changes, has been the one that has finally served me. Thank you!
Answer by zach-r-d · Jul 16, 2015 at 10:46 AM
Please do all of the following:
Delete the semicolon at the end of the if statement (line 11).
Change the function back to OnTriggerEnter2D again. OnCollision*** is only called for two non-triggers.
Make sure the player object is tagged Player.
Make sure that collisions are enabled between the layers that the player and the scene change trigger are on.
5
. $$anonymous$$ake sure the object which must collide has a Rigidbody component
Good catch! Should probably edit to say Rigidbody2D though.
It still doesn't work. I tried everything from the last answer too. I did everything as you said as well but no results. And by number 4. you mean that I set the objects in a way their colliders will touch? If you mean that yes I did that too. $$anonymous$$aybe it's because my player is using rays to detect collision and slopes so the movement is smooth. I really don't know anymore. If it helps I'll post the scripts my player uses but I can't here because of the character limit.
Number 4 means looking at the layer the player is in (the one next to the tag), looking at the layer the trigger is in, then going to Edit -> Project Settings -> Physics 2D and the checkbox indicated by those two layers is checked. Without posting code, could you briefly describe how the player uses rays to detect collision, and equally importantly, how it responds to those collisions?
Nah dude I got it working finally, the script was alright everything I typed was good I did everything you said but and it wasn't working untill I saw the Rigidbody comment. And I added the rigidbody2D to the object that has the script on and it works :D thanks for the help man! You really saved me from a lot of rage.
Answer by Hexer · Jul 16, 2015 at 11:17 AM
void OnColliderEnter2D (Collider2D collider)
{
if(collider.tag == "Player")
Application.LoadLevel("testingscene2");
}
I wouldn't know why you would put "testingscene2" as a string to level. but your code is fine.
make sure your quad has a rigidbody2D attached to it. and set the gravity Scale to zero. That is if you want to not make use of gravity for that object.
Make sure the trigger is enabled on just 1 of the objects. (not 2, just 1)
EDIT : Add Debug.Log("its Colliding!);
to make sure it is working.
Yeah man I got it working it was the rigidbody2D component that was missing. I added it to the player and now it works.
Answer by Johnz1234 · Jul 16, 2015 at 10:39 AM
void OnTriggerEnter2D(){
Application.LoadLevel(2);
}
and check if the object where the script is put have a collider and set it to trigger :D
Answer by basim15 · Aug 16, 2016 at 07:01 AM
using UnityEngine; using System.Collections;
public class levelchange : MonoBehaviour {
void OnColliderEnter2D (Collider2D collider)
{
if(collider.tag == "Player")
Application.LoadLevel("level1");
Debug.Log("its Colliding!");
}
}
this is my code but nothing is happening i have no clue why, i have a box collider with is trigger on my object and i've also loaded the scenes in build settings