Search Unity

Possible Issue/Bug/Oddity: Collision of a child GameObject with another child GameObject

Discussion in 'Scripting' started by robnw, Nov 22, 2017.

  1. robnw

    robnw

    Joined:
    Sep 1, 2017
    Posts:
    4
    Not sure if this is a bug, intentional, or etc, but I believe I came across a bit of confusing usage:

    When I was using the OnCollisionEnter2D(Collision2D other) hook, I tried to call other.gameObject.tag and it seemed to refer to the parent of the gameObject I was colliding with. I looked up how to "get tag of collision" and the solution was to just use other.collider.tag, which did refer to the child gameObject. Please correct me if I am wrong, but I believe this is an inconsistency?

    The scenario: I had two parent gameObjects, A and B respectively, each with their own child gameObjects, AA, AB, and BA, BB. When AA collides with BA or BB, the output below occurs.

    Code (CSharp):
    1. void OnCollisionEnter2D(Collision2D other)
    2. {
    3.     Debug.Assert(other.gameObject == other.collider.gameObject);
    4.     Debug.Log(other.gameObject.tag);
    5.     Debug.Log(other.collider.gameObject.tag); //or other.collider.tag
    6. }
    7. output:
    8. Assertion failed
    9. "Parent"
    10. "Child"
    I tested it a bit and this doesn't happen with OnTriggerEnter2D(Collider2D). Also, if AA collides with other objects within the same hierarchy as A, this doesn't seem to occur. This only appears to occur on child objects in different hierarchies (as far as I've tested).

    I just found this a bit interesting, possibly confusing for some, and I am assuming it is a bug. This doesn't seem like a big deal, but to anyone that might be up all night over this, I hope you found this post helpful.
     
    Last edited: Nov 22, 2017
  2. szymonisd

    szymonisd

    Joined:
    Mar 29, 2023
    Posts:
    1
    Thx u man