Unity 在对象脚本之间共享布尔值

发布于 2024-12-19 00:20:12 字数 162 浏览 3 评论 0原文

我试图创造一个谜题,将一个盒子移动到压力板上时会打开一扇门。当我将盒子放在压力板上时,我试图将布尔值传递给大门,告诉它打开。这是实现我的目标的正确方法吗?

我有 Unity 版本 3.4,它不是专业版,我正在使用 C# 进行编码。

任何帮助都很感激,因为我刚刚开始尝试学习团结。

I was trying to create a puzzle where a box when moved onto a pressure plate opens a gate. When I get the box onto the pressure plate I was trying to pass a Boolean to the gate telling it to open. Is this the correct way of achieving my goals?

I have unity version 3.4 it is not pro and i'm coding in C#.

Any help appreciated as I have just started trying to learn unity.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

好多鱼好多余 2024-12-26 00:20:12

假设您的门游戏对象在层次结构视图中的名称为 MagicGate,并且分配了一个 GateController 类型的脚本组件,其中包含以下代码:

public class GateController : MonoBehaviour {
    // Awake(), Update(), ...
    public void OpenGate ();

    public void OpenGate () {
      // code for opening the gate
    }

可能还有另一个类 GameStatusController,您可以在其中检测到盒子位于魔法压力板处(碰撞器对此很有用)。在那里你可以:

GameObject player = GameObject.Find ("MagicGate");
GateController gateController = player.GetComponent<GateController> ();
gateController.OpenGate ();

Let's say your gate game object has the name MagicGate in your hierarchy view and has a script component assigned of type GateController containing the following code:

public class GateController : MonoBehaviour {
    // Awake(), Update(), ...
    public void OpenGate ();

    public void OpenGate () {
      // code for opening the gate
    }

There is another class maybe GameStatusController where you detect that the box is at the magic pressure plate (Colliders are useful for this). There you do:

GameObject player = GameObject.Find ("MagicGate");
GateController gateController = player.GetComponent<GateController> ();
gateController.OpenGate ();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文