Author Topic: Prefab losing target - specifying parameter ?  (Read 2226 times)

DarJam

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Prefab losing target - specifying parameter ?
« on: November 02, 2014, 05:53:05 PM »
My prefab containing UIButton and onClick notify with target where i want the object clicked in one of many to be passed to a target. Unity removes the on click information from the prefab after the first use. This was answered on another message but that did not include a parameter. How do i code it with a parameter?

  1. table[i] = (GameObject) Instantiate( Resources.Load( "Prefabs/NGUI/Table/tabledef_basic"));
  2. EventDelegate.Add( table[i].GetComponent<UIButton>().onClick, FixturesPressed);    
  3. EventDelegate.Parameter = table[i];
  4.  
  5. public void FixturesPressed(GameObject go){
  6.    Debug.Log ( go.name + " clicked!");
  7. }
  8.  

The compiler generates

error CS1502: The best overloaded method match for `EventDelegate.Add(System.Collections.Generic.List<EventDelegate>, EventDelegate)' has some invalid arguments
Error CS1503: Argument `#2' cannot convert `method group' expression to type `EventDelegate'

It works fine without a parameter but i need a parameter. Can anyone help ? Thanks

DarJam

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: Prefab losing target - specifying parameter ?
« Reply #1 on: November 03, 2014, 04:39:05 AM »
OK, this type of question is answered on other posts.My problem was a table of game objects with variable information and size and when clicked i wanted it to pass a message to a higher object and i needed to know which one was tapped. I found this worked well.

  1. ...
  2. EventDelegate del = new EventDelegate(this, "FixturesPressed");
  3. del.parameters[0] = new EventDelegate.Parameter(go, "gameObject");
  4. table[i].GetComponent<UIButton>().onClick.Add ( del );
  5. ...
  6. public void FixturesPressed (GameObject go)
  7. {
  8. Debug.Log(go.name);
  9. }
  10.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Prefab losing target - specifying parameter ?
« Reply #2 on: November 03, 2014, 06:55:13 AM »
Same result, much less code:
  1. EventDelegate.Add(button.onClick, delegate () { Debug.Log(go.name); });