Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: bbfy on April 23, 2017, 03:39:22 AM

Title: UIButton added via script not responding to clicks
Post by: bbfy 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?
Title: Re: UIButton added via script not responding to clicks
Post by: ArenMook 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.
Title: Re: UIButton added via script not responding to clicks
Post by: bbfy 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)
Title: Re: UIButton added via script not responding to clicks
Post by: ArenMook 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.