- Home /
Unity4.6 OnTriggerEnter2D is not called constantly
My player object has 1 rigidbody2D (not kinematic), 1 circle collider, 1 box collider attached and tagged as "Player". My pickup has a trigger circle collider.
I instantiate several pickups in my scene, some can be picked up but some can't.
This is the code attached on pickups
void OnTriggerEnter2D (Collider2D other)
{
if (other.tag == "Player")
gameObject.SetActive (false);
}
If anyone could help, that would be very appreciated.
i think you should start debugging, > Check if all of your colliders are 2D > use debug statements, > A gameobject can have only one collider of either normal or 2D, since you are telling you have 2 colliders attached, check which collider has proper tag > use OnTriggerStay2D() for debugging
The sample asset has a 2D character which has 2 colliders too, and it works well. All colliders are 2D, since all objects come from one prefab
multiple collider component on same GameObject do not work.
If you want to add multiple colliders on same GameObject . Add them as child of that GameObject.
@NaRango4 @Priyanshu Nothing changes after I remove one of the collider. Some coins are picked up, some are not.
Does the "Player" also have a OnTriggerEnter2D somewhere? If so, it might be that the "Player" takes the trigger event and therefor the pickup doesnt get the trigger message.
Try something like this, to see if the OnTriggerEnter2D actually gets called when you collide:
void OnTriggerEnter2D (Collider2D other)
{
if (other.tag == "Player")
gameObject.SetActive (false);
else
Debug.Log("Collision but not with player");
}
Answer by SilentSin · Jan 14, 2015 at 10:48 AM
Your title doesn't seem to reflect your actual problem, you don't want OnTriggerEnter to be called repeatedly, just once for every object.
Try adding:
else
{
Debug.Log("WTF did I hit? " + other, other);
Debug.Break();
}
The second parameter tells Unity which object to highlight when you click on the log message and the Debug.Break() will pause the game when it happens. So you'll get a log line and single clicking it will highlight the object you hit in the hierarchy.
If it never logs that line, then there must be something different about the pickups its not working on. Are their colliders disabled? Are they on the wrong layer? Are their colliders offset to some weird position?
All pickups come from one prefab and dynamically spawned by one script. I can't think of anything reason those pickups won't be identical. Someone else already mentioned your debug code, and as I said, codes in the else statement never got executed
Why do you have both a circle and a square collider on your player? Are they both on the same object or is one on a child object?
Because I don't konw how to make the character move smoothly by only using a box collider. I have to add a circle collider at the bottom.
The circle collider is in a child object. Actually I tested both situation. When 2 colliders are on the same object, roughly half of the pickups will be missed. Once I move the circle collider to the child object, it reduces to about 2/10.
Are both the box and circle tagged as player? $$anonymous$$aybe its triggering on the one that isn't tagged then not triggering on the other because they share the same rigidbody?
Yes, they are. If it's triggered by an untagged collider, I would get the debug message.
Your answer
Follow this Question
Related Questions
Child Trigger event on parent Script 1 Answer
OnTriggerEnter2D Not Working 1 Answer
OnTriggerEnter2D is not being called 1 Answer
score display not showing 1 Answer