OnTriggerEnter/Stay/Exit Question

Hello! I think I’m a little confused on how the OnTriggerEnter/Stay/Exit functions work. Here’s the facts:

  • I have one GameObject that has a collider which is a trigger. We’ll call this Object A.
  • I have another GameObject that has a collider and a rigidbody. We’ll call this Object B.
  • Both Object A and Object B have the OnTriggerEnter(Collider other) function.
  • When Object B enters Object A’s trigger collider, both OnTriggerEnters are called.
  • Object A OnTriggerEnter’s other.name is Object B.
  • Object B OnTriggerEnter’s other.name is Object B.

This appears to be because OnTriggerEnter always passes the object that caused the trigger to go off, regardless of which call of OnTriggerEnter it is.

Is this true? Is there any way for Object B to be aware of Object A?

Thanks for your time!

I couldn’t reproduce that. When i set up a simple scene with the conditions you listed, i always get the name of the other object.

The scene:
I added a standard-cube and a sphere, leaving the names as they are.
The collider of the cube is set to be a trigger, the sphere has got a rigidbody attached to it.

When i add a very simple script to both objects that only logs the other.name to the console, i also get the name of both, sphere and cube.

Object B OnTriggerEnter’s other.name is Object B.

I didn’t expect that result. Could you post your code?

@Suddoha - What version of Unity are you using? I’m currently using the latest stable 4.6 beta.
@SkillBased - I’ll see if I can make a simplified version of what I’m making to post. It’s really an odd result.

I haven’t updated Unity yet and the code i was using for the test is

public class TriggerTest: MonoBehaviour
{
  void OnTriggerEnter(Collider trigger)
  {
  Debug.Log(trigger.name);
  }
}

Turns out I was just dumb. I did that test above and it worked fine. Come to find out, since I have a class hierarchy that handles the collisions, I was passing collider in as the argument, not the OnTriggerEnter(Collider other) variables. All is well now. Thanks for the replies! :smile:

1 Like