Is it possible to get a reference to the object that entered the trigger in the OnTriggerEnter method?

  • There was recently a similar question ... and therefore clarification, what do you mean by the words который вошёл в триггер ? The one that was outside the trigger, went and now literally enters the trigger? Or has the object once entered a trigger, is already there and you want to get it? - Alexey Shimansky

1 answer 1

The answer is yes, you can get. Because OnTriggerEnter is just called when the Collider another object enters the trigger.

 void OnTriggerEnter(Collider other) { GameObject enteredObject = other.gameObject; Debug.Log(enteredObject.name); } 

This message is sent to the trigger, and to its rigidbody (if any), and to the rigidbody (or collider, if there is no rigidbody ) that are in contact with the trigger.

Please note that trigger events are sent only if one of the colliders also has an attached rigidbody

What information can be obtained from the collider can be viewed at the docks in the Inherited members :

  • gameObject - just the object that entered the trigger
  • tag - object tag
  • transform
  • hideFlags
  • name - the name of the object
  • other