简体   繁体   English

从父对象访问位于子对象上的脚本组件中的方法

[英]Accessing from parent object a method from a script component located on a child

I've got an object player on which I've put a child which contains only a script casting a line to detect if the player is grounded. 我有一个对象播放器,上面放了一个孩子,该孩子只包含一个脚本,该脚本会投射一行以检测播放器是否接地。 I'd like to get the result of this linecast in my playerControls script (component of player). 我想在我的playerControls脚本(播放器的组件)中获得此广播的结果。 To do this i've made a public methods that returns the result of the linecast, but I don't know how to make a reference to the script to call the method. 为此,我制作了一个公共方法,该方法返回线播的结果,但是我不知道如何引用脚本来调用该方法。

How can I do ? 我能怎么做 ?

https://docs.unity3d.com/ScriptReference/Component.GetComponentsInChildren.html https://docs.unity3d.com/ScriptReference/Component.GetComponentsInChildren.html

 hingeJoints = GetComponentsInChildren<HingeJoint>();

If you only have one player you can also use a Singleton pattern. 如果您只有一个玩家,则也可以使用Singleton模式。 But the easier solution is to get the component of the child. 但是更简单的解决方案是获取孩子的组成部分。

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

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