统一获取游戏对象的子对象
Getting child object of gameObject in unity
我有对象 "Unit" 和子对象“怪物和健康。
我还有带有球体对撞机的对象塔。
我还在 Tower Object 中有 OnTriggerEnter(Collider co) 函数来检测 Unit。
当它出现时,我可以通过访问它来打印名称 "Unit"
co.gameObject.name,甚至co.name,我猜也是一样的。
但是我怎样才能得到单元的第一个子对象呢。我的意思是 Monster 对象,但不是名字,而是 Unit 对象的第一个子对象?
更新
使用此代码:
void OnTriggerEnter(Collider co)
{
Debug.Log(co.gameObject.transform.GetChild(0));
}
导致异常:
UnityException: Transform child out of bounds
Tower.OnTriggerEnter (UnityEngine.Collider co) (at Assets/Scripts/Tower.cs:19)
更新 2
打印(co.transform.childCount);给出 2
这是正确的,因为我有
Unit
>
Monster
HealthBar
子对象
更新 3
塔代码。
使用统一引擎;
使用 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;
}
}
}
这段代码以某种方式打印了两次
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)
您必须对 GameObject 的 transform
进行操作。您可以使用 Transform.GetChild(int index)
函数。
您可能首先必须检查是否有任何子项,因为如果您超出数组范围,GetChild 会抛出异常。为此,您必须使用 Transform.childCount
.
可在此处找到更多信息:
http://docs.unity3d.com/ScriptReference/Transform.GetChild.html
http://docs.unity3d.com/ScriptReference/Transform-childCount.html
我有对象 "Unit" 和子对象“怪物和健康。 我还有带有球体对撞机的对象塔。 我还在 Tower Object 中有 OnTriggerEnter(Collider co) 函数来检测 Unit。
当它出现时,我可以通过访问它来打印名称 "Unit" co.gameObject.name,甚至co.name,我猜也是一样的。
但是我怎样才能得到单元的第一个子对象呢。我的意思是 Monster 对象,但不是名字,而是 Unit 对象的第一个子对象?
更新
使用此代码:
void OnTriggerEnter(Collider co)
{
Debug.Log(co.gameObject.transform.GetChild(0));
}
导致异常:
UnityException: Transform child out of bounds
Tower.OnTriggerEnter (UnityEngine.Collider co) (at Assets/Scripts/Tower.cs:19)
更新 2 打印(co.transform.childCount);给出 2
这是正确的,因为我有
Unit
>
Monster
HealthBar
子对象
更新 3 塔代码。 使用统一引擎; 使用 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;
}
}
}
这段代码以某种方式打印了两次
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)
您必须对 GameObject 的 transform
进行操作。您可以使用 Transform.GetChild(int index)
函数。
您可能首先必须检查是否有任何子项,因为如果您超出数组范围,GetChild 会抛出异常。为此,您必须使用 Transform.childCount
.
可在此处找到更多信息:
http://docs.unity3d.com/ScriptReference/Transform.GetChild.html http://docs.unity3d.com/ScriptReference/Transform-childCount.html