Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

OnTriggerEnter/Stay/Exit Question

Discussion in 'Scripting' started by barinelg, Sep 4, 2014.

  1. barinelg

    barinelg

    Joined:
    Jun 1, 2010
    Posts:
    95
    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!
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    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.
     
  3. SkillBased

    SkillBased

    Joined:
    Aug 11, 2014
    Posts:
    141
    Object B OnTriggerEnter's other.name is Object B.

    I didn't expect that result. Could you post your code?
     
  4. barinelg

    barinelg

    Joined:
    Jun 1, 2010
    Posts:
    95
    @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.
     
  5. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    I haven't updated Unity yet and the code i was using for the test is

    Code (csharp):
    1.  
    2. public class TriggerTest: MonoBehaviour
    3. {
    4.   void OnTriggerEnter(Collider trigger)
    5.   {
    6.   Debug.Log(trigger.name);
    7.   }
    8. }
    9.  
     
  6. barinelg

    barinelg

    Joined:
    Jun 1, 2010
    Posts:
    95
    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! :D
     
    Suddoha likes this.