Skip to content

Instantly share code, notes, and snippets.

@Ddemon26
Created April 30, 2025 02:00
Show Gist options
  • Save Ddemon26/19793c515309d0eaadfb9d7c274bd8f5 to your computer and use it in GitHub Desktop.
Save Ddemon26/19793c515309d0eaadfb9d7c274bd8f5 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class NewMonoBehaviourScript : MonoBehaviour {
public List<Button> buttons = new List<Button>();
void Awake() {
foreach (var button in buttons) {
button.onClick.AddListener( OnButtonClick );
}
}
void OnButtonClick() {
//log the position in the list of the button that was clicked
int index = buttons.IndexOf
(
(Button)UnityEngine.EventSystems
.EventSystem.current.currentSelectedGameObject
.GetComponent<Button>()
);
Debug.Log( "Button clicked: " + index );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment