Search Unity

Button listener not working like intended

Discussion in 'Scripting' started by BubyMB, Dec 30, 2018.

  1. BubyMB

    BubyMB

    Joined:
    Jun 6, 2016
    Posts:
    140
    Hey guys,
    Code (csharp):
    1.  
    2. private void Update()
    3.     {
    4.         if (player != null)
    5.         {
    6.             skt = new SkillTree();
    7.             skt = skt.GetTree(player.skillTree);
    8.             upgrades = skt.GetTreeUpgrades(player.skillTree);
    9.         }
    10.  
    11.         for (int i = 0; i < 8; i++)
    12.         {
    13.             GameObject holder = GameObject.Find("Holder " + (i + 1));
    14.             foreach (Transform go in holder.transform)
    15.             {
    16.                 switch (go.name)
    17.                 {
    18.                     case "skillName":
    19.                         go.GetComponent<Text>().text = UpgradeNames.getName(i, player.classSelection);
    20.                         break;
    21.                     case "Button":
    22.                         go.GetComponent<Button>().onClick.RemoveAllListeners();
    23.                         go.GetComponent<Button>().onClick.AddListener(delegate { upgradeItem(i); });
    24.                         break;
    25.                     case "upgradeDisplay":
    26.                         go.GetComponent<Text>().text = "(" + upgrades[i] + "/4)";
    27.                         break;
    28.                 }
    29.             }
    30.         }
    31.     }
    32.  
    33.     public void upgradeItem(int i)
    34.     {
    35.         Debug.Log("Clicked" + i);
    36.         if (player.skillPoints >= 1)
    37.         {
    38.             if (skt.GetTreeUpgrades(skt.SaveTree(skt))[i - 1] < 4)
    39.             {
    40.                 skt = skt.GetTree(skt.increaseUpgrade(skt.SaveTree(skt), i - 1));
    41.                 player.skillTree = skt.SaveTree(skt);
    42.                 player.skillPoints--;
    43.             }
    44.         }
    45.     }
    46.  
    So I have this small code which is supposed to iterate through each holder and add a listener for the upgradeItem function, but for some reason it always returns 8. For example, I will press the 3rd button (which should return 3) But instead it returns 8, same with all other buttons.
    I added a Debug.log at the "button case" for i and it returns the correct numbers. I feel like im doing something stupid anything help is welcomed :)
     
    Last edited: Dec 30, 2018
  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    553
    Code (CSharp):
    1. go.GetComponent<Button>().onClick.AddListener(delegate { upgradeItem(i); });
    This is where the problem lies. The delegate is referencing i and it is taking the very last value of i which would be 8.

    A way around this is to create a local variable that the delegate will reference instead of the changing value of i.
    Code (CSharp):
    1. int j = i;
    2. go.GetComponent<Button>().onClick.AddListener(delegate { upgradeItem(j); });
     
  3. BubyMB

    BubyMB

    Joined:
    Jun 6, 2016
    Posts:
    140
    Working perfectly now, Thanks.