After adding the script at Button OnClick, I can't find the method I wrote myself

  1. Preface
    Record a Unity Button event error. After writing the script, and adding the Button component to the object, when adding the OnClick event to it, I found that the method written in the script could not be found, as shown in the figure:
    insert image description here
  2. Problem Analysis
    2.1 Troubleshooting of the script
    After inspection, there is no compilation error in the script, and it inherits from MonoBehavior.
    2.2 Script mounting problem
    After consulting the data, the above problem is caused by directly mounting the script to OnClick. OnClick should mount the GameObject object containing the script to realize it. The operation is as shown in the figure:
    1. Mount the script on the parent object
    insert image description here
    2. Mount the GameObject on OnClick
    insert image description here
    3. Find the method! The effect is as shown in the figure
    insert image description here

Guess you like

Origin blog.csdn.net/weixin_47952981/article/details/129676862