Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Button AddListener Issue [FIXED]

Discussion in 'UGUI & TextMesh Pro' started by Black-pearl, Jan 10, 2015.

  1. Black-pearl

    Black-pearl

    Joined:
    Mar 20, 2014
    Posts:
    55
    Hey everyone

    I have a little problem with a button script i've made basically when the button is pressed it will subtract just the item amounts required, however on click it is carrying out approximatly 800 operations per click

    Is there a way around this?

    Code (csharp):
    1.  
    2.  
    3. Button button = tooltip.transform.GetChild(4).GetComponent<Button>();
    4.   button.onClick.RemoveAllListeners();
    5.   button.onClick.AddListener(delegate {StartCoroutine(Create(craft));});
    6.  
    7. IEnumerator Create(Crafts craft)
    8.   {
    9.   yield return new WaitForSeconds(0.1f);
    10.   inventory.addItem(craft.itemreference);
    11.   Resourcesubtract(craft.craftWood, craft.craftStone, craft.craftMetal);
    12.   }
    13.  
    Thanks in advance
     
  2. Black-pearl

    Black-pearl

    Joined:
    Mar 20, 2014
    Posts:
    55
    Fixed... Seemed to be a bug with unity not updating my scripts in the editor the coroutine was the fix :)
     
  3. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    That syntax was obsolete as of November 2007.
    Code (csharp):
    1.  button.onClick.AddListener( () => StartCoroutine(Create(craft)) );