0

Hi I have a public List of buttons. And I want to get index of a button which is in this list when we clicked it. How can I do this, thanks?

redokum
  • 21
  • 3
  • 1
    Does this answer your question? [How to use IndexOf() method of List](https://stackoverflow.com/questions/1568593/how-to-use-indexof-method-of-listobject) – BugFinder Oct 09 '22 at 19:08
  • check this too https://stackoverflow.com/questions/49317488/how-i-can-get-gameobject-index-in-the-list – Yugerten Oct 09 '22 at 19:51
  • Please provide enough code so others can better understand or reproduce the problem. – Community Oct 10 '22 at 05:49

2 Answers2

0
public static Button clickedButton;
private List<Button> buttonList;
private int index;

for (i == 0; i < buttonList.Count; i++)
{
   if (buttonList[i] == clickedButton)
   {
      index = i;
   }
}

public void GetClickedButton()
{
   clickedButton = this.gameObject.GetComponent<Button>();
   // u should add this in the onclick event of the button
}
MrBlender
  • 197
  • 14
0
for (int i = 0; i < btns.count; i++)
{
    int enclosureVar = i;
    btns[i].onClick.AddListener(FindIndex(i));
}

private _selectedBtnIndex = 0;

public void FindIndex(int index)
{
    _selectedBtnIndex = index;
}

//this may contain some errors but this method method will work for you.
Haluk Özgen
  • 11
  • 1
  • 1