Unity UI 按钮不调用 Onclick 方法

Unity UI Button not calling the Onclick method

我在 canvas 下有一个按钮可以重新启动场景。但是它没有调用附加脚本的预期功能。

在 Unity 论坛上,我发现解决方案应该在 canvas 中处于更高层次,并且 canvas 应该有图形光线投射等等。

但它仍然无法正常工作,尽管它的点击数组检测到它应该调用的预期方法。

已选择 Canvas 的场景编辑器:

选择了按钮的场景编辑器:

  1. 从按钮中删除 canvas 组件。
  2. 确保层次结构中某处有一个 GraphicRaycaster on your canvas, and that there is an EventSystem 对象。

(当您第一次添加 canvas 时,两者都应该被添加,但是事情丢失了)

显示你的按钮被选中的屏幕截图还显示,你确实为 OnClick 事件选择了一个游戏对象,但你没有从该字段旁边的下拉列表中选择一个功能,它说 no function.

Instead of selecting the C# class, select the GameObject that btnSceneSelect is attached to.

If btnSceneSelect is not attached to a GameObject, attach it to one (other than the button).

取自this site