Author Topic: UIButton added via script not responding to clicks  (Read 1837 times)

bbfy

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
UIButton added via script not responding to clicks
« on: April 23, 2017, 03:39:22 AM »
Hi all,

I have created am adding the prefab with attached uibutton script via script. The button has a script where I am adding the click event.

  1. void Start() {
  2.                 UIButton uiButton = gameObject.GetComponent<UIButton>();
  3.                 EventDelegate.Set(uiButton.onClick, onClickHandler);
  4.         }
  5.  
  6.         public void onClickHandler() {
  7.                 Debug.Log("bla");
  8.         }
  9.  

The button i added in a table inside a scroll view, if this matters. However if I add the button manually, it works fine, but if I add it via script in does not respond on clicks.

any idea?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIButton added via script not responding to clicks
« Reply #1 on: April 23, 2017, 06:23:17 AM »
Check the layer the object is on, make sure it has a collider, and that the collider is properly sized.

bbfy

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: UIButton added via script not responding to clicks
« Reply #2 on: May 04, 2017, 07:17:36 AM »
@ArenMook I followed this Tutorial:

https://www.youtube.com/watch?v=UK3aMHRfgcw

So I do have a layer, a WidgetLayer to scroll. What is really unusual, If I add the items manually, so before run, clicks are working. Right now I removed this layer to scroll and have a scrollbar (ugly solution)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIButton added via script not responding to clicks
« Reply #3 on: May 04, 2017, 05:00:35 PM »
If one way is working and the other doesn't, it should be pretty easy to check them in inspector to see what differs. :)

How are you adding whatever it is you're adding? Did you double-check their layers and make sure the transform's parent is what it should be? If you set your parent manually after Start(), (such as when using a pool of objects) use NGUITools.MarkParentAsChanged() to notify NGUI that the parent has been changed.