手抄报 安全手抄报 手抄报内容 手抄报图片 英语手抄报 清明节手抄报 节约用水手抄报

unity 脚本中如何获得另外物体的数据

时间:2024-10-13 01:24:17

1、打开unity3d选择菜单中的“GameObject”--“3D Object”。

unity 脚本中如何获得另外物体的数据

2、在场景里面创建两个物体,一个Cube、一个Sphere。

unity 脚本中如何获得另外物体的数据

3、接着我们创建两个脚本,一个Cube_data、一个Sphere_data。

unity 脚本中如何获得另外物体的数据

4、把脚本分别对应放在两个物体上面。

unity 脚本中如何获得另外物体的数据
unity 脚本中如何获得另外物体的数据

5、在Sphere_data脚本中创建一个变量sphere_aa=100,作为要被获得的数据。

unity 脚本中如何获得另外物体的数据

6、在Cube_data脚本的start中通过GameObject.Find获得并输出烂瘀佐栾这个sphere_aa变量的数值(旎髂坑若同样方式我们也可以获得其它组件属性)。void Start () { int aa =GameObject.Find("Sphere").GetComponent<Sphere_data>().sphere_aa; print(""+aa); }

unity 脚本中如何获得另外物体的数据

7、运行游戏场景我们发现终端会输出变量的值100,Cube_data脚本获得Sphere物体上的数据成功。

unity 脚本中如何获得另外物体的数据
© 手抄报圈