- Home /
The question is answered, right answer was accepted
OnTriggerEnter not working, tried everything! :( (C#)
Hi, I've only spent a week using Unity so far so very new! Trying to make an arcade-style falling game within a tunnel. I can't get this to work, despite having used this before successfully.
void OnTriggerEnter(Collider other)
{
print("YAY!");
if(other.gameObject.tag == "Finish" && finished == false){
if(terminal == true){
win_text.text = "YOU WIN!";
} else {
lose_text.text = "YOU LOSE...";
}
finished = true;
} else if (other.gameObject.tag == "Obstacle"){
// can add response to collision with obstacles.
}
}
To my disappointment, the phrase "YAY!" is never printed, and there is no interaction with the various GUIText elements.
As you can see, I tried fixing it by having the GUIText not constantly refresh (it finishes with constant contact) by adding an extra boolean, but it wasn't even being run in the first place.
The player (sphere, Sphere Collider) has this script attached to it, and the finish (cube, Box Collider) has the tag "Finish". The player has a Rigidbody, the finish doesn't.
I've been stuck on this for hours now and looking at similar posts I can't resolve the problem. Please help!
Answer by OLP · Jul 21, 2014 at 06:48 PM
For your objects to receive the OnTriggerEnter, at least one of them has to have the Is Trigger property checked and at least one of them has to have a Rigid Body. If neither of your objects is a Trigger, you can use OnCollisionEnter instead.
Once that's all set, you should check the Layers (not Tags) on your objects. To edit which Layers collide with each other, you can look at Edit -> Project Settings -> Physics.
By default Unity sets all layers to collide with all layers. That's a good works-by-default setup, but you may want to play with it to optimize later on.
You should probably search for the collision table.I think you can see it on the BoxCollider manual page.There it says what kinds of colliders can successfully collide with others.That means kinematic,static,rigidbody,triggers whatever.It will be incredibly helpful down the road.
Totally forgot about adding the RigidBody. Thanks for the re$$anonymous$$der :)
This also nicely works in combination with OnTriggerEnter2D (Collider2D other) .. and again you need to have the RigidBody2D + box collider2D (with is trigger) added to the gameobject too.
i've been stuck on this for a while and only just noticed that I had to use OnTriggerEnter2D vs OnTriggerEnter, other's having trouble that just can't seem to figure it out make sure you are using the right function call if you are in 2D
other than all mentioned above (rigidbody, isTrigger), both objects should have colliders assigned to them as well.
Answer by 432qwerty · Jul 21, 2014 at 08:10 PM
Thanks for the response, it's really helped me understand this better, but I found the issue... I was using the wrong function! I should have used onCollisionEnter all along, but thanks for the advice!
This isn't the answer, please select the correct answer and revert this to a comment.
Answer by manzyron · May 26, 2016 at 09:48 PM
Hi in 5.3.4 both objects need to have rigid body
That's wrong. Only one needs a rigidbody. Check twice before you write an answer.
this awnser helped me :) thanks... either one or the other needs a rigid body... was scratching my head for a while over this haha
It is true. I have a rigidbody2d on my zombie object and no rigidbody2d on my arrow object. Arrow doesn't detect any ontrigger2d collisions until rigidbody2d to it is assigned
then there must be some other issue in your project because of a collision only one object needs a rigidbody.
Follow this Question
Related Questions
How do I stop a timer with OnTriggerEnter()? 0 Answers
Collision Checking 1 Answer
Adding Force to a Rigidbody through OnTriggerEnter 1 Answer
Dragging object out of position 2 Answers