0

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.

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.

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 ?

UPDATE

Using this code:

void OnTriggerEnter(Collider co)
{
    Debug.Log(co.gameObject.transform.GetChild(0));
}

Causes an exception:

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

UPDATE 2 print(co.transform.childCount); gives 2

And that's correct cause I have

Unit
>

Monster

HealthBar

subobjects

Update 3 Tower code. using UnityEngine; using System.Collections;

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<Bullet>().target = co.transform;
        }
    }
}

This Code Somehow manages to print twice

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)
David
  • 4,332
  • 13
  • 54
  • 93
  • Out of bounds means that there are no children in the transform. You'll have to check that first. See http://docs.unity3d.com/ScriptReference/Transform-childCount.html – MarengoHue Mar 01 '16 at 11:23
  • I've checked and I receive 2 which means that I trully have 2 subobjects. What can I do now ? – David Mar 01 '16 at 11:43
  • Are you sure that you are working with correct gameobject? co in OnTriggerEnter(Collider co) is the entering entity. Show us all of your code. – MarengoHue Mar 01 '16 at 11:46
  • Actually when I ran simulation once more, I see in debug 2 messages. First 2, and then at the same time 0. How come ? – David Mar 01 '16 at 11:49
  • The Above code somehow prints twice in debug. first "2" then "0". And I have only one print!!! :( – David Mar 01 '16 at 11:52
  • I believe that debugging your solution is beyond the scope of this question. The problem may be in that you have attached this 'Tower' script to the item that collides with another tower. You'll have to check your Unity project for that. – MarengoHue Mar 01 '16 at 12:22
  • 1
    Possible duplicate of [How can I find child gameobject?](http://stackoverflow.com/questions/25763587/how-can-i-find-child-gameobject) – Fattie Mar 01 '16 at 12:30
  • Try not to answer or comment on questions that are million-times duplicates. One of thousands of informative QA on this topic .. http://stackoverflow.com/a/27389984/294884 The clutter on this tag is huge and has to be reduced guys – Fattie Mar 01 '16 at 12:31

1 Answers1

2

You'll have to operate on GameObject's transform. You can use Transform.GetChild(int index) function.

You will probably first have to check if there are any children because GetChild throws exceptions if you get out of bounds of the array. For that you'll have to use Transform.childCount.

More info can be found here:

http://docs.unity3d.com/ScriptReference/Transform.GetChild.html http://docs.unity3d.com/ScriptReference/Transform-childCount.html

MarengoHue
  • 1,789
  • 13
  • 34
  • It worked. problem was with bullet object which secondly called print. Great thanks for the Help! – David Mar 01 '16 at 12:43