1、静态方法
(1)根据对象名(游戏物体)查找对象
GameObject obj2 = GameObject.Find("Holens");if( obj2 != null ){print(obj2.name);}else{print("没有找到对应对象");}
(2)通过tag来查找对象
GameObject obj3 = GameObject.FindWithTag("Player");
(3)查找多个对象
GameObject[] objs = GameObject.FindGameObjectsWithTag("Player");
print("找到tag为Player对象的个数" + objs.Length);
!!!注意:这些方法都无法找到失活的对象!!!
(4)摧毁对象
GameObject.Destroy(myObj2);//第二个参数 代表延迟几秒钟删除GameObject.Destroy(obj5, 5);//Destroy不仅可以删除对象 还可以删除脚本GameObject.Destroy(this);
如果类继承了Mono,那可以不写GameObject
2、成员方法
(1)为对象添加脚本
Lesson2 les2 = obj6.AddComponent<Lesson2>();
(2)标签比较
if(this.gameObject.CompareTag("Player")){print("对象的标签 是 Player");}
(3)设置激活失活
obj6.SetActive(false);
obj6.SetActive(true);