简体   繁体   English

指向Unity3d中脚本的指针

[英]Pointer to a script in Unity3d

I have a gameObject and I want to get a value from one of the scripts. 我有一个gameObject,我想从其中一个脚本中获取一个值。 How would I point to the script? 我将如何指向脚本? At the moment I am calling the gameObject from a script named Body and I want to get the script named RightArm. 目前,我正在从名为Body的脚本中调用gameObject,我想获取名为RightArm的脚本。

Inside your Body script you can do this, assuming that the Arm is inside the Body. 在“身体”脚本中,您可以执行此操作,假设手臂在身体内部。

RightArm rArm = GetComponentInChildren<RightArm>();

It will look through all the Children and find the correct component you want. 它会浏览所有子项,并找到所需的正确组件。 You can use this to find all sorts of Components, such as Rigibodies. 您可以使用它来查找各种组件,例如Rigibodies。

Assuming the RightArm is somewhere else, maybe on the floor outside the body. 假设RightArm在其他地方,也许在身体外面的地板上。 Then you can do this: 然后,您可以执行以下操作:

RightArm rArm = GameObject.Find("RightArm").GetComponent<RightArm>();

Let "RightArm" be the name of the GameObject containing the RightArm Script. 令“ RightArm”为包含RightArm脚本的GameObject的名称。

This second implementation will look through in the scene for the GameObject named "RightArm" then look at the components in that gameObject and return you the RightArm script. 第二个实现将在场景中浏览名为“ RightArm”的GameObject,然后查看该gameObject中的组件,并返回RightArm脚本。 If there is no gameObject named "RightArm" then you will get an exception when trying to get a component of null. 如果没有名为“ RightArm”的游戏对象,则在尝试获取null组件时会出现异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM