Unity 5.2 Buttons OnClick() function not running.

I am adding a button (or a text box with an added button component) to a canvas (which has an Event System). I then add an empty game object and add script to it…

using UnityEngine;
using System.Collections;

public class Doo : MonoBehaviour {

	public void NextLevel()

I add this to the button by adding an OnClick() event and dragging the game object to it and finally selecting the function from the drop down.

When I run it, the OnClick() function never runs.

My button is as follows:


Any ideas???

I had the same issue and for me to fix it all I had to do was leave unity and relaunch the project and it worked. I would give it a try then message me back and let me know if it worked.

Your setup should work, I tried it and the debug run. Do you see the button highlighting? (you probably do since it’s red and hard to miss) Can you connect the button to some other simple function and test?

Same issue here. Editor relaunch doesn’t seem to do the trick for me.


@Liveo - I may have a workaround.

Make sure that in your Touch Input Module, “Force Module Active” is unchecked. It seems like it might be an issue with raycast processing order. Also, try re-ordering child GameObjects if you have trouble with any UI elements. We had most our UI up and running, but a few elements were still broken. Once we moved those child elements lower in our list, they seemed to fire back up and work.

FWIW - it looks like a bug that should be corrected in newer releases.

Hope this helps,

i was facing the same issue, the issue was due to event system. in my project event system was merged with canvas object, i solved it by removing Event System script and Standalone Input module from my canvas and created new Event System after that it is working fine for me.

I know this is necro, but just in case anyone else had my problem:

One of my UI elements was flipped 180 degrees on the Y, so it wasn’t able to receive pointer clicks. Hope this helps someone!

this is how look my Button and i want to work with on click but i foun videos only with on click like the second picture can somewho help me?

I have the same issue too,but I add a new button.Suddenly,everything is working. But I do not know Why.

I had the same issue and move my button to the bottom of the components in the Canvas hierarchy solved the issue, but still sometimes need to Play it a few times until the button works properly.

My Unity Editor: 5.3.5f1 Personal

Hope this get fixed soon.

Try to play with the Z-axis of a camera and move it back and forth.162594-unity-z2jh2pwnwy.png

I just deleted the button and created a new one, and it worked for me.

I should have said that the button highlights when you move over it, but does not change colour when I press it (even when the ‘Pressed Colour’ is changed). I have tried it with other functions and I get nothing. I also get nothing when I add a breakpoint.

I have also rebuilt the whole thing varying everything I can think of. Still nothing!!!