Andreas Widtfeldt Andreas Widtfeldt - 3 months ago 32
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:
http://forum.unity3d.com/threads/unity-5-4-button-auto-clicks.426526/

Answer

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 : https://unity3d.com/learn/tutorials/topics/user-interface-ui/ui-button

EDIT :

Please learn more about scripting here: https://unity3d.com/learn/tutorials/topics/scripting