我有一个游戏对象,我想从其中一个脚本中获取一个值。我将如何指向脚本?目前我正在从名为 Body 的脚本中调用 gameObject,并且我想获取名为 RightArm 的脚本。
1 回答
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.
Assuming the RightArm is somewhere else, maybe on the floor outside the body. Then you can do this:
RightArm rArm = GameObject.Find("RightArm").GetComponent<RightArm>();
Let "RightArm" be the name of the GameObject containing the RightArm Script.
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. If there is no gameObject named "RightArm" then you will get an exception when trying to get a component of null.