Search Unity

[SOLVED] Can't access a child through collision...

Discussion in 'Scripting' started by Iron-Warrior, Mar 7, 2010.

  1. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    838
    Howdy Unity,

    Just a quick question here. Building a space shooter, adding in "motherships" (larger enemies). I want to make certain parts of them vulnerable to damage. To do this, I add a Script called "ArmouredDamage" to the main mothership gameObject which has an exposed array of gameObjects. Each of the vulnerable spots is a gameObject that is a child of the mothership. Both the mothership and the vulnerable spots have colliders attached to them.

    Anyways. The point is I use a really basic script to make all enemies take damage...it is...

    Code (csharp):
    1. var health = 100.0;
    2. var explosion : ParticleEmitter;
    3.  
    4. function TakeDamage (damage) {
    5.     health -= damage;
    6.     Debug.Log("HIT!");
    7.     if (health <= 0)
    8.     {
    9.         if (explosion)
    10.         {
    11.             Instantiate (explosion, transform.position, transform.rotation);
    12.         }
    13.         Destroy(gameObject);           
    14.     }
    15. }
    This. Whenever a laser collides with a collider, it does this...

    Code (csharp):
    1. function OnCollisionEnter (collision : Collision) {
    2.     Kill();
    3.     collision.gameObject.SendMessage("TakeDamage", damage, SendMessageOptions.DontRequireReceiver);
    4.    
    5. }
    6.  
    7. function Kill()
    8. {
    9.     Destroy(gameObject);
    10. }
    My problem is the children gameobjects won't take damage! I'm guessing this has to do with the SendMessage call or something, but I can't really find any info on it. I know that there's nothing wrong with the children gameobjects (I detached them from the main mothership and they take damage just fine) so it has to be something along the lines that SendMessage isn't recieving them because they're children or something...I know the lasers ARE colliding with the vulernable spots for sure, but the message isn't being recieved.

    Thanks for any help guys!
     
  2. ooppari

    ooppari

    Joined:
    Jan 29, 2009
    Posts:
    68
    Use BroadcastMessage, which calls the method named methodName on every MonoBehaviour in this game object or any of its children.

    SendMessage Calls the method named methodName on every MonoBehaviour in this game object.
     
  3. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    838
    I know, but I only want to call the method in that one child gameObject.

    I found out what the problem is, but not how to solve it. When the laser collides with the child (the vulnerable spot) gameObject's collider, the Collision.gameObject that is returned is the parent (mothership). I just want to call the method in that one child, but I'm not sure how to access it from the collision data.
     
  4. ooppari

    ooppari

    Joined:
    Jan 29, 2009
    Posts:
    68
    Your mothership has collider and laser is hitting to that, but not child object collider which is inside mothership collider? Is that the case? If it's then why don't use just increase a little but child objects collider so that it's hitting first on that one, and not the mother ship. Or do you need to get that mothership collision also?
     
  5. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    838
    It's hitting the child game object for sure, but the collision data returns the parent.

    After doing some testing and debugging it turns out the fact that I had a rigidbody attached to the parent gameObject was causing the problem. If I remove the rigidbody, the child properly returns the collision...I need the rigidbody attached to the parent so I can manipulate it's velocity, so does anyone know why this happens?

    EDIT: In case anyone views this and wants an answer, I solved it.

    Code (csharp):
    1.     for (c in collision.contacts)
    2.     {
    3.         c.otherCollider.gameObject.SendMessage("TakeDamage", damage, SendMessageOptions.DontRequireReceiver);
    4.     }
    This cycles through each contact point, gets the gameObject and sends the message.