在Unity3d中获取游戏对象有三种方法:
一:获取对象
1.通过对象名称获取:objCube=GameObject.Find”Cube”);
private var objCube:GameObject;
private var isCubeRoate=false;
function Start ) {
objCube=GameObject.Find”Cube”);
}
function Update){
ifisCubeRoate){
objCube.transform.Rotate0.0f,Time.deltaTime*200,0.0f);
}
}
function OnGUI){
ifGUILayout.Button”旋转”,GUILayout.Height50))){
isCubeRoate=true;
}
}
2.通过tag标签获取单个游戏对象:objCube=GameObject.FindWithTag”Finish”);
3.通过游戏标签获取多组游戏对象:objCube=GameObject.FindGameObjectsWithTag”Finish”);
二:子对象
//获取所有子对象
foreach Transform child in transform)
{
Debug.Logchild.gameObject.name);
}
//销毁所有子对象
foreachTransform child in transform){
Destroychild.gameObject);
}