Author Topic: NGUI Adding OnClick delegate through script?  (Read 5898 times)

Tripwire

  • Full Member
  • ***
  • Thank You
  • -Given: 9
  • -Receive: 0
  • Posts: 163
    • View Profile
NGUI Adding OnClick delegate through script?
« on: January 14, 2014, 10:54:19 AM »
So I was doing some coding and I need to add an OnClick delegate through function, because the button is a prefab. Only to find out the documentation isn't really helping here. I've tried doing:
Documentation:
  1. UIEventListener.Get(gameObject).onClick += MyClickFunction;
  2.  

My code
  1. public void FoodItemClicked(GameObject go)
  2. {
  3.         Debug.Log ("Clicked");
  4. }
  5.  
  6. for(int x = 0; x < FoodList.Count; x++)
  7. {
  8.         GameObject clone = Instantiate(ProductListItem, ProductListItem.transform.position, Quaternion.identity) as GameObject;
  9.         SetParent (clone);
  10.         clone.transform.localPosition = new Vector3(0, 276 - (100 * listCount), 0);
  11.         clone.GetComponentInChildren<UILabel>().text = FoodList[x].ToString();
  12.         UIEventListener.Get(clone).onClick(clone) += FoodItemClicked; /*<- This line*/
  13.         listCount++;
  14. }
  15.  
The FoodItemClicked function is in the same script as the for loop.
As stated in the Documentation I shouldn't need to add a GameObject to the OnClick function but since it's a Void Delegate which needs a GameObject Go I figured this should be it. Now i'm getting an error message stating the following:
Quote
Assets/Scripts/Managers/Product Search/ProductSearchManager.cs(51,52): error CS0019: Operator `+=' cannot be applied to operands of type `void' and `method group'
Assets/Scripts/Managers/Product Search/ProductSearchManager.cs(51,52): error CS0131: The left-hand side of an assignment must be a variable, a property or an indexer

I'm doing something wrong what I can't figure out :(

lzt120

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 68
    • View Profile
Re: NGUI Adding OnClick delegate through script?
« Reply #1 on: January 14, 2014, 08:18:24 PM »
UIEventListener.Get(clone).onClick+= FoodItemClicked;