Ok, I am stumped on this one. I’ve looked through many examples and cannot figure out what is going wrong with this one… so it’s probably something simple to make me look bad
I am attempting to generate a grid of buttons on a panel and add a listener to each button. Ideally I want to make it so that each button will change color when selected, but I’d be happy with just a debug output at this point.
I started by creating a button prefab, emptied the text (the text object is still there, just empty), and made it a square. Then I attached it to my script along with a panel I created. (script below) I am seeing the grid of the buttons like I want, but I cannot get them to trigger the listener.
Anyone know what I’m doing wrong here? Any help would be much appreciated!
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class SetupBoard : MonoBehaviour
{
[SerializeField]
private GameObject _cellPrefab;
[SerializeField]
private RectTransform _panel;
[SerializeField]
private float _rez = 10;
[SerializeField]
private float _buffer = 0.1f;
// Start is called before the first frame update
void Start()
{
Vector2 panelOffset = new Vector2(_panel.rect.width / 2, _panel.rect.height / 2);
for(int x=0; x<_rez; x++)
{
for(int y=0; y<_rez; y++)
{
GameObject cell = (GameObject)Instantiate(_cellPrefab);
cell.transform.SetParent(_panel, false);
Vector3 cellSize = new Vector3(_panel.sizeDelta.x/_rez/100, _panel.sizeDelta.y/_rez/100, 1f);
cell.transform.localScale = cellSize;
cell.transform.localPosition = new Vector3(
x*((cellSize.x*100)+_buffer) - panelOffset.x,
y*((cellSize.y*100)+_buffer) - panelOffset.y,
0f);
cell.GetComponent<Button>().onClick.AddListener(() => OnCellClick(x, y));
}
}
}
void OnCellClick(int x, int y)
{
Debug.Log("Cell (" + x + "," + y + ") selected");
}
// Update is called once per frame
void Update()
{
}
}