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

 时间:2026-02-14 01:51:46

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 脚本中如何获得另外物体的数据

  • unity怎么打开资源商店
  • Unity怎么导出游戏为可执行文件#校园分享#
  • Unity如何导出资源包#校园分享#
  • Unity3D没有控制台(Console)窗口怎么办?
  • Uinty 3D如何添加脚本
  • 热门搜索
    interview是什么意思 什么是劣药 人死了是什么感觉 什么是猝死 运动会跳高加油稿 大专学什么专业好 经期喝什么好 波尔多液是什么 物流是什么意思 什么是软件工程