【问题标题】:enable script when press button in unity统一按下按钮时启用脚本
【发布时间】:2021-03-16 12:20:03
【问题描述】:

我如何在统一按下特定按钮时启用和禁用敌人对象上的脚本

void OnButtonClick()
{
    StartCoroutine(Cooldown());
    GetComponent<FollowEnemy>().enabled = !GetComponent<FollowEnemy>().enabled;
}

【问题讨论】:

  • 您的代码有什么问题?一般来说你应该缓存GetComponent的结果

标签: c# unity3d


【解决方案1】:
GetComponent(MyScript).enabled = false;

也许是个不错的起点,

对于按钮Input.GetKey(KeyCode.Space),就可以了。

您可能需要尝试一下,您可以在此处找到更多信息:

https://answers.unity.com/questions/26844/enabledisable-specific-components.html

官方文档:

https://docs.unity3d.com/ScriptReference/KeyCode.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-03
    • 1970-01-01
    • 2021-12-21
    • 2021-04-20
    • 2014-07-09
    相关资源
    最近更新 更多