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

2025-10-25 14:49:39

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

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢