- Home /
why isn't my OnTriggerEnter2D() function working?
guys im struggling over an hour on the 2d collision event. Currently I have 2 sprite's each have a 2d boxcollider and are triggers. I have set the tag of one of the sprite's to "player" but it still doesn't work. I have already checked everything ten times and searched the internet for a solution.....without succes.
please help a fellow unity for the sake of the community xd
btw both sprite's are animated, but I don't think that's the problem...
using UnityEngine;
using System.Collections;
public class collision : MonoBehaviour {
void OnTriggerEnter2D (Collider2D other)
{
if (other.gameObject.tag == "Player")
{
Debug.Log ("why wont you work ;_;");
}
}
}
[1]: /storage/temp/19090-capture2.png
Answer by arkariarn · Dec 08, 2013 at 11:31 PM
After hours of testing i finally got it to work here is how:
first you need one of the two objects to contain a 2d rigidbody. While i was already doing this it still didn't work. In my game i'm a bird so I didn't want gravity to effect my player so I had set the kinematic to true. This caused the object to ignore collision events.... so I just set the gravity scale of the object to zero and made the object dynamic (not-kinematic).
this fixed my problems... hope it helps somebody.....ever....
Another potential gotcha that I have encountered with collision triggers not working is if the Physics2D settings don't have interactions turned on for the the layers in question. So, for example, if you have objects in user defined layers such as Player and $$anonymous$$issile, but you don't have the check box marked for Player/$$anonymous$$issile collisions, then no triggers will be called. You can check your settings in Edit/Project Settings/Physics2D.
Still doesnt work for me, its set for 2D an using js, is trigger has be ticked for the chosen ones, they all have a Rigidbody2D and a circle collider, scripts all '2D's at the end of 'collider' and 'OnTriggeEnter-Exit'
What can I do?
This is stupid ; I had the same problem (two collider2D, one was as trigger). I fixed it by adding a rigidbody2d component to one of my colliders. I don't even need any rigidbody behavior. Anyone knows why this is necessary ? Can't we just have colliders collide/trigger by themselves without rigidbody involved ?
kinematic means it is not affected by forces period not just gravity
Answer by abdextrous · Mar 15, 2017 at 09:23 AM
In my case it was the most stupid one I guess (Sorry a newbie in CS, and also was following a poor quality video tutorial !). I had a typo mistake, made it onTriggerEnter2D instead of OnTriggerEnter2D :(
Not for functions like these. Essentially if it isn't an override function, then what Unity does is it scoures the class file for all functions, and if one of those compares to a default function, it adds it to the event handler of the specific events. In other words, it doesn't care what you call your methods, but if it is a default one, it will run it, otherwise only your code will run it when you call it. There is no "error" to be had here.
... also check if your parameter is correct. For example, it needs to be a Collider2D for OnTrigger...2D, not a Collider.
Good this is my case kk !! The code pattern is different and confusing in unity. Becaus has a many different patterns in world ok. But commonly the programers using camelCase. I prefer UpperCamelCase its more legibly and intuitive. But Unity merge the two styles it's very confusing
I've just spent the better part of an hour trying to figure this issue out, and your comment was a god-send. Thank you!
Answer by RogerDebian · Apr 28, 2017 at 05:51 AM
I forget the damn Rigidbody2d. In my case, my player has a Character Controller, so, there is a problema between the Rigidbody2d and the Character controler, so, I made a new gameobject with a rigidbody2d and a box collider and BOM! solution!
thanks for your research! I forgot the behavior of the components in this case! :D
Answer by SoliderSpy · May 01, 2018 at 09:11 PM
You don't need to make gravity scale 0. Put a rigidbody2d on both of the objects and then you will be able to make them both kinematic with the collision or trigger working. :)
Thank you. Worked for me. However, make sure "simulated" box is ticked.
Answer by hersheys72 · May 02, 2016 at 01:09 AM
Thanks for your answer. For mine the box collider size had defaulted to 1 by 1, when the object itself was 400 x 400, so it was never colliding. Also had to change the offset to match the objects location but works fine now :)
Your answer
Follow this Question
Related Questions
Why doesn't OnTriggerEnter2D get called? 1 Answer
Wanting to have a sprite animation to only start playing once the player collides with it? 1 Answer
Unity2D: Renderer off/ on by triggers 0 Answers
Collision with renderer.enabled? 0 Answers
Check if the center of an object is inside a trigger Collider? 1 Answer