I have a button that invokes a modal window (a market transaction). Using the inspector onclick, I have successfully setup so that 1 of the 9 buttons will pop the window (GameObject.SetActive ✅), and then the Done button in the window will close it (🟩).

However there are 9 of these buttons, and they use a prefab for their design.

Here's a pic of the hierarchy with the button instance highlighted:

enter image description here

Each rowItem is the exact same (from a rowItem prefab). Also, each row has data that needs to be passed into the modal. An index would probably be enough.

How do I setup the prefab so that the 9 instanced buttons invoke the popup and pass the data?

I'm a JS webdev by day, so I generally understand the concepts. I'm just newbie on the syntax and where everything is in Unity.

  • \$\begingroup\$ Please add images directly to your question (which uses Stack's built-in hosting) rather than linking to 3rd party hosting as it helps prevent link rot. \$\endgroup\$
    – Pikalek
    Sep 6 at 4:42

1 Answer 1


I'm not 100% sure I understand your question, but if you want to get the index of the button you pressed, you can do something like this:

public class MarketTable : MonoBehaviour {
    [SerializeField] private IndexedButton buttonPrefab;
    private List<IndexedButton> buttons;

    void Awake() {

    void AddButtons() {
        buttons = new List<Button>(9);
        for (int i = 0; i < 9; i++) {
            var button = Instantiate(buttonPrefab);
            button.Index = i;

    void OnClickButton(IndexedButton button) {
        int index = button.Index;
        //do something with the index here

[System.Serializable] public class IndexedButtonEvent : UnityEvent<IndexedButton>

public class IndexedButton : MonoBehavior {
    [SerializeField] private IndexedButtonEvent clickedEvent = new IndexedButtonEvent();

    public int Index { get; set; }
    public IndexedButtonEvent ClickedEvent => clickedEvent;

    void Awake() {
        button = GetComponent<Button>();

    void OnClickButton() {

Here we've created a custom component for the Market Table UI panel and a custom component for our buttons. We add the IndexedButton component to our button prefab. When we instantiate a button, we set its index. When we click a button, the button's ClickedEvent is invoked, which calls the OnClickButton() function with a reference to the button that was clicked. We can then check the index on the button.

You could also replace the Index property with some other type of data that you want to associate with the button.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .