Andreas Widtfeldt Andreas Widtfeldt - 1 year ago 252
C# Question

Unity 5.4 Button auto-clicks

When I create a button in unity3d 5.4 the button just clicks automatic when i press play in the editor. I haven't been using unity before, so I can't say if it would happen in other versions.

First i create a script, then I attatch it to a empty gameobject. Then I press the little + sign on the OnClick() in the button properties, so I can add the gameobject with the script attached.

I have screenshots of button properties, eventSystem, Canvas, gameobject and the Script.

If anyone know what i have done wrong, please let me know. Thanks in advance.

The screenshots are in this post, since I cant post 5 images in stackoverflow:

Answer Source

When you press play in editor, Button is NOT pressed at all. Its just that you have Debug.Log("Clicked!") in Start() method. Start() is called by Unity automatically when you run the application. It has nothing to do with button click. You need to register OnClick() listener method so it will be called when you press the button.

Have a look at this tutorial :


Please learn more about scripting here:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download