Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Resolved OnTrigger not working despite working elsewhere in project.

Discussion in 'Scripting' started by jarosgregory, Jul 31, 2022.

  1. jarosgregory


    May 9, 2022
    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.

    Code (CSharp):
    1. private void OnTriggerEnter2D(Collider2D collision)
    2. {
    3.          print("In!");
    4. }
    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.
    Code (CSharp):
    1. GameObject auraObj = new GameObject("Aura", typeof(ParticleSystem), typeof(CapsuleCollider2D), typeof(Rigidbody2D));
    2. auraObj.layer = LayerMask.NameToLayer("Aura");
    3. auraObj.transform.SetParent(gameObject.transform, false);
    5. auraCol = auraObj.GetComponent<CapsuleCollider2D>();
    6. partsys = auraObj.GetComponent<ParticleSystem>();
    7. auraBody = auraObj.GetComponent<Rigidbody2D>();
    9. auraCol.isTrigger = true;
    10. auraCol.size = new Vector2(5f, 5f);
    12. 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!

    Attached Files:

  2. RadRedPanda


    May 9, 2018
    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.
  3. jarosgregory


    May 9, 2022
    Yeah, that's not it. Every box is checked for each layer interaction. At least that's another thing checked off.
  4. jarosgregory


    May 9, 2022
    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.
  5. mopthrow


    May 8, 2020
    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.
  6. Kurt-Dekker


    Mar 16, 2013
    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.
    mopthrow likes this.