I have a struct array of toggles defined. I am adding a listener to each of them. Now I would like to know which toggle was changed when user presses on a toggle to change the value. How do I know from my script which toggle was changed and use it as an index?
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class ToggleData : MonoBehaviour {
[System.Serializable]
public struct toggleInfo {
public Toggle toggle;
public TextMeshProUGUI toggleText;
}
public toggleInfo[] toggles;
public int toggleIndex = 0;
// Start is called before the first frame update
void Start () {
for (int i = 0; i < toggles.Length; i++) {
toggles[i].toggle.onValueChanged.AddListener (delegate {
ToggleValueChanged (toggles[i].toggle);
});
}
}
void ToggleValueChanged (Toggle change) {
Debug.Log ("toggle changed " + toggleIndex); //Get the index here
}
}