- Home /
Collision To Next Level Is Not Working
#pragma strict
function OnCollisionEnter(collision: Collision) {
if (collision.gameObject.tag == "Exit"){
Application.LoadLevel ("Level_2");
}
}
the idea of this code is to have my player hit the cube(which is tagged "Exit"), and then the game will switch to another level called Level_2
The problem is the collision just doesn't work. My script is attached to my player which is the standard first person controller.
It should work as long as you make sure your cube has a mesh collider and that your loadlevel function is working. What I would do is: make the cube a trigger and put this script on it:
function OnTriggerEnter(col: Collision) {
if(col.tag == "Player" || col.gameObject.name == "First Person Controller") {
Application.LoadLevel("Level_2");
}
}
I have not tested this, and JavaScript isn't exactly my field of expertice. So here is the C# version. Just put it on your exit.
void OnTriggerEnter(Collider c) {
if(c.tag == "Player")
Application.LoadLevel("Level_2");
}
If this doesn't work you could try OnTriggerStay. And if that doesn't work either, something is definently wrong with the cubes collider or the scene youre trying to load!
okay ill try all 3 things that you suggested then ill tell you ow they go
Answer by KravenArk · Jun 26, 2015 at 09:08 PM
The cube has a box collider on it so that isn't an issue. I couldn't use the first script because I got this error "Tag is not a member of UnityEngine.Collision" The second script had no effect. I'm attaching my scene so you can see for yourself. http://www.filedropper.com/colliderdoesntwork
Change col.tag to col.transform.tag or something like that. Anyways; I got some time to test it now. If you want the script to be on the player, you can simply use OnControllerColliderHit! Here is a C# sample that I've tested and it works:
using UnityEngine;
public class Trigger : $$anonymous$$onoBehaviour {
void OnControllerColliderHit(ControllerColliderHit other) {
if(other.collider.tag == "Exit")
Application.LoadLevel("Test");
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613201434im_/https://answers.unity.com/themes/thub/images/avi.jpg)