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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

how to delete buttons listener

Discussion in 'Scripting' started by makelchs, Mar 9, 2022.

  1. makelchs

    makelchs

    Joined:
    Feb 4, 2022
    Posts:
    12
    I'm making three button that gives player random upgrade selection
    So when player level up script choose random upgrade and
    put diffrent onClick.AddListener for each button
    and when I click button
    onClick.RemoveListener activate to clear button for next level up
    but problem is button which I didn't select
    onClick.RemoveListener only affect in button which I select
    so another button's listener is still remaining and it makes wrong upgrade result in next level up
    Sadly I don't have any idea how to remove unclicked button's listener
    Is there any way to remove all button's listener when I choose upgrade?

    here is my script sample for upgrade
    Code (CSharp):
    1. //Onclick add listener by switch
    2.  
    3.             case "ATK Upgrade":
    4.                 B1.onClick.AddListener(ATKLevelUp);
    5.                 b = "null";
    6.                 break;
    7.  
    8. //ATK upgrade
    9.  
    10.     void ATKLevelUp()
    11.     {
    12.         if (StatManager.LevelChanged == false)
    13.         {
    14.             StatManager.ATK = StatManager.ATK + 1f;
    15.             StatManager.LevelChanged = true;
    16.         }
    17.         B1.onClick.RemoveListener(ATKLevelUp);
    18.     }
     
  2. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    777
    Have you tried B2.onClick.RemoveAllListeners(); ?
     
  3. makelchs

    makelchs

    Joined:
    Feb 4, 2022
    Posts:
    12
    yes Button2 also have Remove listener
    but if I select button1 at level up
    then b2.onClick dosen't work because there is only one chance to click button per level up :(