Search Unity

OnTriggerEnter Event

Discussion in 'Scripting' started by kdubnz, Nov 11, 2019.

  1. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
    Just an observation. In 2019.3.0b10 (untested in others)
    In the attached Project the only GameObject with IsTrigger toggled ON is the Sphere.
    The TriggerTrap script is attached to all GameObjects in the Scene.
    The Sphere reports a TriggerEnter collision with the Cube and the Plane
    The Cube and Plane report a TriggerEnter collision with the Sphere
    Note the output in the console.
    The docs https://docs.unity3d.com/2019.3/Documentation/Manual/CollidersOverview.html
    say :

    >> The scripting system can detect when collisions occur and initiate actions using the OnCollisionEnter function. However, you can also use the physics engine simply to detect when one collider enters the space of another without creating a collision. A collider configured as a Trigger (using the Is Trigger property) does not behave as a solid object and will simply allow other colliders to pass through. When a collider enters its space, a trigger will call the OnTriggerEnter function on the trigger object’s scripts
    .

    https://gyazo.com/1009745a8fd48109c145ae3eeee82c08

    The call seems to be being made on the function on the scripts on the 'other' objects.
    Is this the expected behaviour. ?




    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class TriggerTrap : MonoBehaviour
    4. {
    5.     private void OnTriggerEnter(Collider other) {
    6.         print($"{this.name} Collision_Trigger with {other.name}" );
    7.     }
    8. }

    Regards,
     

    Attached Files:

    Last edited: Nov 11, 2019
  2. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
    My 'guess' is that the trigger<->trigger collider issue which is "resolved" in 2019.3.0b10 MAY be causing this situation.
     
  3. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    If both objects have scripts that have OnTriggerEnter, both will receive the event. This has been the behavior as far as I can tell since I started using Unity.
     
    Gunging likes this.
  4. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
    Thank you.
    Looks like the Unity documentation needs to be modified to match that.
    Regards,