OnTrigger not working despite working elsewhere in project.

I’ve used OnTrigger in elsewhere in this project but for some reason this one isn’t working.

I want to trigger some code when the Player object enters the collision sphere of the Apple’s Aura child object (see screenshot attached). Right now that code is just a print statement and as you can see there is no console output.

private void OnTriggerEnter2D(Collider2D collision)
{
         print("In!");
}

When I refer to “both objects” below, these are the two in question. This is how it is set up so far and I have two inspector windows for both objects pulled up in the screenshot so you can see for yourself:

  • I have a RigidBody2D on both objects (Player’s is dynamic, the Aura’s is static).
  • Both objects have a 2D Collider (square on the Player, capsule on the Aura).
  • The Aura’s collider is enabled as a trigger.
  • There is a script on the Aura’s parent (Apple), which generates the Aura itself with the RigidBody2D and CapsuleCollider2D and sets it as a trigger. This script also has the trigger function above in it. Relevant excerpt (from the start function) from said script below.
GameObject auraObj = new GameObject("Aura", typeof(ParticleSystem), typeof(CapsuleCollider2D), typeof(Rigidbody2D));
auraObj.layer = LayerMask.NameToLayer("Aura");
auraObj.transform.SetParent(gameObject.transform, false);
    
auraCol = auraObj.GetComponent<CapsuleCollider2D>();
partsys = auraObj.GetComponent<ParticleSystem>();
auraBody = auraObj.GetComponent<Rigidbody2D>();
    
auraCol.isTrigger = true;
auraCol.size = new Vector2(5f, 5f);
    
auraBody.bodyType = RigidbodyType2D.Static;

So overall there is something small that I’m just ignorant about which is keeping this from working. Please give me more possibilities to check off. Thank you all in advance for your help!

I didn’t really see anything that stuck out to me…

Slim chance, but is it possible you set the collision layer matrix to not collide with each other? It should be under Edit(maybe?) > Project Settings > Physics.

Yeah, that’s not it. Every box is checked for each layer interaction. At least that’s another thing checked off.

I think I get why it’s not working. So I have the OnTrigger function attached to the trigger object. I’m guessing it has to be on the object entering the collision. So if I move the OnTrigger in to my Player object’ script it works, which is fine but I’d rather that code be on the Aura instead.

Hmm this shouldn’t be the case. Static or dynamic, whether the mover or the non-mover has the script with the trigger function on it, the OnTrigger function should still fire.

Have you checked the bounds of the colliders? Maybe they’ve been displaced relative to your graphics and aren’t colliding as expected.

Just to cover all bases:

With Physics (or Physics2D), never manipulate the Transform directly. If you manipulate the Transform directly, you are bypassing the physics system and you can reasonably expect glitching and missed collisions and other physics mayhem.

Always use the .MovePosition() and .MoveRotation() methods on the Rigidbody (or Rigidbody2D) instance in order to move or rotate things. Doing this keeps the physics system informed about what is going on.

1 Like