Author Topic: Button firing click twice  (Read 3268 times)

pad406

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 62
    • View Profile
Button firing click twice
« on: February 26, 2014, 12:42:40 PM »
I noticed that a button I have was firing twice. After spending ages trying to figure out what I'd done, I created a new scene, added a 2D UI and created a lable to which I attached a collider and button script. Another simple script to Debug.Log("You clicked") and hey presto one click debug displayed twice. Some searching mentioned Unity Remote (not using that) and Event Sources on UICamera, so disabled all but Mouse. Still happening. Not really affecting my project but thought you might like to know. It may affect some.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Button firing click twice
« Reply #1 on: February 26, 2014, 02:39:01 PM »
On which target platform and what version of NGUI?

pad406

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 62
    • View Profile
Re: Button firing click twice
« Reply #2 on: February 26, 2014, 03:05:06 PM »
NGUI 3.5.1 only update yesterday. Have tried on web, android and iOS, all them same. Steps taken to reproduce
Unity -> New Project
Only import NGUI
Create label
add collider
add button script
add my script
set notify
run

My script

using UnityEngine;
using System.Collections;

public class tstScript : MonoBehaviour
{

    public void OnClick()
    {
        Debug.Log("You clicked");
    }

}

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Button firing click twice
« Reply #3 on: February 26, 2014, 03:08:00 PM »
Oh, well yeah that would do it.

OnClick is a generic NGUI event. It gets triggered regardless of whether you set the notification or not.

UIButton notification (On Click notification) is separate, and is itself using the NGUI OnClick event to trigger a remote function of your choice.

So the first time OnClick fires is because your script is being called directly by the NGUI event system. The second time it fires is because UIButton is calling it because you set it as the notification target.

pad406

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 62
    • View Profile
Re: Button firing click twice
« Reply #4 on: February 26, 2014, 04:14:19 PM »
Ok, makes sense. I misunderstood that and thought you always had to set the notification, now I know better  ;D