Getting child object of gameObject in unity
<p>I have Object "Unit" with sub Objects "Monster and Health.
I also have Object Tower that has sphere collider.
Also I have OnTriggerEnter(Collider co) function in Tower Object that detects Unit.</p>

<p>When it does I can for example print the name "Unit" by accessing it
co.gameObject.name, or even co.name, which I guess is the same.</p>

<p>But how can I get the first subobject of unit for example. I mean Monster object, but not by name but just the FIRST SUB OBJECT of Unit object ?</p>

<p><strong><em>UPDATE</em></strong></p>

<p>Using this code:</p>

<pre><code>void OnTriggerEnter(Collider co)
{
    Debug.Log(co.gameObject.transform.GetChild(0));
}
</code></pre>

<p>Causes an exception:</p>

<pre><code>UnityException: Transform child out of bounds
Tower.OnTriggerEnter (UnityEngine.Collider co) (at Assets/Scripts/Tower.cs:19)
</code></pre>

<p><strong>UPDATE 2</strong>
print(co.transform.childCount); gives 2</p>

<p>And that's correct cause I have</p>

<pre><code>Unit
&gt;

Monster

HealthBar
</code></pre>

<p>subobjects</p>

<p><strong>Update 3</strong>
Tower code.
    using UnityEngine;
    using System.Collections;</p>

<pre><code>public class Tower : MonoBehaviour
{
    // The Bullet
    public GameObject Bullet;

    // Rotation Speed
    public float rotationSpeed = 35;

    void Update()
    {
        transform.Rotate(Vector3.up * Time.deltaTime * rotationSpeed, Space.World);
    }

    void OnTriggerEnter(Collider co)
    {

        print(co.transform.childCount);

        if (co.gameObject.name == "Unit(Clone)")
        { 

            GameObject g = (GameObject)Instantiate(Bullet, transform.position, Quaternion.identity);
            g.GetComponent&lt;Bullet&gt;().target = co.transform;
        }
    }
}
</code></pre>

<p>This Code Somehow manages to print twice</p>

<pre><code>2
UnityEngine.MonoBehaviour:print(Object)
Tower:OnTriggerEnter(Collider) (at Assets/Scripts/Tower.cs:20)
0
UnityEngine.MonoBehaviour:print(Object)
Tower:OnTriggerEnter(Collider) (at Assets/Scripts/Tower.cs:20)
</code></pre>