【问题标题】:Cannot Enable/Disable Button(Script) Component of UI Button in Unity无法在 Unity 中启用/禁用 UI 按钮的按钮(脚本)组件
【发布时间】:2017-05-08 07:20:42
【问题描述】:

我想要访问 UI Button 的组件并想要启用禁用的组件“ Button(Script)" 用于解锁下一关,但不启用组件。

if(PlayerPrefs.GetInt("Level") == 1){
   Button Level2 = Unlock.GetComponent<Button>();
   Level2.enabled = true;
}

P.S:我的代码中没有语法错误。

【问题讨论】:

  • 调试代码,然后用结果编辑您的问题。没有它很难帮助你。检查Level2 是否为空,通过将Debug.Log(Level2) 放入该if 语句中。这还将告诉您if 该语句是否正在运行。
  • 您也可以尝试将按钮的 interacrable 属性设置为 false 或 true。
  • 谢谢@cjf93 你的评论节省了我的时间..
  • 我作为答案发布,也许它适用于其他人

标签: c# user-interface unity3d


【解决方案1】:

您还可以将按钮的可交互属性设置为 true o false。

Button myButton;
myButton.interactable = true;
//Here your button works normal

myButton.interactable = false;
//The click on your button is disabled here

【讨论】:

  • 谢谢我已经用你提到的方法解决了问题
  • 我会为其他人添加一些东西,你不应该禁用整个组件,而是使用组件的变量作为整个组件启用/禁用选项。
【解决方案2】:

确保 if 块被执行,确保你的“解锁”-我猜的游戏对象-上面有一个按钮组件。 没有语法错误并不意味着你没有概念错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多