Hi!
I am making a system where I can have a group of buttons, and when one is clicked it should become uninteractable, and the others become interactable again (basically, only one button can be "selected" at once).
The problem is that I get an error when clicking one of the buttons (see attached image). It's on line 25: "btns[_btnClickedIndex].interactable = false;"
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ButtonGroup : MonoBehaviour
{
[SerializeField] private Button[] btns;
private void Start()
{
for (int i = 0; i < btns.Length; i++)
{
btns[i].onClick.AddListener(() => { ButtonInGroupClicked(i); });
}
}
public void ButtonInGroupClicked(int _btnClickedIndex)
{
for (int i = 0; i < btns.Length; i++)
{
btns[i].interactable = true;
}
btns[_btnClickedIndex].interactable = false;
}
}