- Home /
On Collide script not working!
So I am making a game where you have to click buttons on the sides to move the cube in the middle from side to side to dodge the other cubes. The bad cubes and the ground are animated to make it move forward, and I have a script to on collide go to the next scene. Its not working and goes rght through the good cube. triggers are on and I attached the script to all of the cubes.
Here is the script:
var level : String; function OnTriggerEnter( hit : Collider){ Application.LoadLevel("FirstLevel"+level); }
Thank you for the help!
Im guessing you have this set up in your scene incorrectly. try turning off the isTrigger property on one of the cubes. also you should have this script attached to one cube, not both of them. Also, if you havent already, add rigidbody components to them
You probably need to add a rigidbody component(colliders doesn't work without it) and you should change your code to:
var level : String;
function OnCollisionEnter(hit : Collision){
Application.LoadLevel("FirstLevel"+level);
}
This also means that whenever the object this script attached to touch any object(not only your player) it will restart the level so to change that you can add:
if(hit.gameObject.tag == "YOUR PLAYER OBJECT TAG"){
Application.LoadLevel("FirstLevel"+level);
}
Answer by rakeshs997 · Jun 26, 2015 at 05:54 AM
Trigger events are only sent if one of the colliders also has a rigidbody attached. Attach rigidbody to your objet then try it will work.
Thanks