Search Unity

best way to get parent object of component

Discussion in 'Scripting' started by dacloo, Jun 21, 2006.

  1. dacloo

    dacloo

    Joined:
    Jun 30, 2005
    Posts:
    469
    Code (csharp):
    1.  
    2. function OnCollisionEnter (other : Collision) {
    3.     Debug.Log("JA");
    4.     if (other.CompareTag ("Player")) {
    5.         Destroy (other.GameObject);
    6.         }
    7.     }
    8.  
    Destroy doesn't work because GameObject is not part of Collision.
    How do I reference to the GameObject that holds the collision component?
    (the gameobject that belongs to 'other')

    THanks!
     
  2. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    other.transform.gameObject

    A bit unintuitive but works fine.

    It would have been other.gameObject, had it existed, by the way. (Notice the case. GameObject is the name of the class. Camel-case gameObject is the variable in Component.)

    -Jon
     
  3. dacloo

    dacloo

    Joined:
    Jun 30, 2005
    Posts:
    469
    thanks! Logical now I see it.

    And thanks for reminding me about the capitals. keep doing that wrong!