How to find the index of the clicked UI button?

Hi,

I want help with picking and adding materials to the Object dynamically

Here is my process:

Will click on a GameObject, The variants of the gameObject will appear in a UI called Customize.
The UI will have Variant buttons and clicking on a particular Variant button, the material associated with that variant has to be applied to GameObject.
The GameObject will have a script and here is what I’ll define the variants:
[123553-scriptimage.jpg*_|123553]

and here is how the UI is
[123554-ui.jpg*_|123554]

Can someone please help me with finding the associated variant materials on click and adding them to the game object?

It is also great if i can just know the index of element and variant

here is the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CSV_ObjectCustomizer1 : MonoBehaviour {

	public List<ProductsYo> Elements = new List<ProductsYo> ();
	private GameObject MainCanvas = null;
	private GameObject CSVItems = null;

	void OnMouseDown(){
		if (Input.GetMouseButtonDown (0)) {
			OnPupulate ();
		}
	}

	void OnPupulate(){
		MainCanvas = GameObject.Find ("CSV_Content");
		if (MainCanvas.transform.childCount>0) {
			ClearChildren ();
		}
		for (int i = 0; i < Elements.Count; i++) {
			GameObject CSV_ItemPanel = Instantiate(Resources.Load ("CSV_ItemPanel") as GameObject);
			if (CSV_ItemPanel.GetComponentInChildren<Text> ().transform.name == "ItemName") {
				CSV_ItemPanel.GetComponentInChildren<Text> ().text = Elements *.ItemName;*
  •  	}*
    
  •  	CSV_ItemPanel.transform.SetParent (MainCanvas.transform, false);*
    

_ for (int j = 0; j < Elements .Variants.Count; j++) {_
Debug.Log (Elements_.ItemName+ Elements .Variants [j].VariantName);_
* GameObject CSV_ItemButton = Instantiate (Resources.Load (“CSV_ItemButton”) as GameObject);
CSV_ItemButton.transform.Find(“CSV_VariantName”).gameObject.GetComponent().text = Elements.Variants[j].VariantName;
CSV_ItemButton.transform.Find (“CSV_VariantIcon”).gameObject.GetComponent ().sprite = Elements .Variants [j].VariantIcon;
CSV_ItemButton.transform.SetParent (CSV_ItemPanel.transform.Find(“CSV_Items”), false);*

* //Here is what i’m stuck:*
* CSV_ItemButton.GetComponent ().onClick.AddListener (() => OnVariantButtonClicked(i,j));
_ }
}*_

* }*

* void ClearChildren(){*
* MainCanvas = GameObject.Find (“CSV_Content”);
_ Transform Children = MainCanvas.GetComponentsInChildren ();
while (MainCanvas.transform.childCount > 0)
{
Transform child = MainCanvas.transform.GetChild(0);
Destroy(child.gameObject);
}
}*_

* //Here is what i’m stuck*
* void OnVariantButtonClicked(int ElementIndex, int VariantIndex){*
* Debug.Log (ElementIndex + “,” + VariantIndex);*

* }*
}

----------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;

[Serializable]
public struct ElementInfo{
* public string VariantName;*
* public Sprite VariantIcon;*
* public Material VariantMaterial;*

}

[Serializable]
public class ProductsYo{
* public string ItemName;*
* public List Variants = new List();*
}

*
*

This is how I do it in C#, I write a handler for gameObject, handler implements interface such as OnTouch() or something, I have only one raycaster, which also has a hashtable, which is used to find the handler, you can write register/deregister methods in raycaster, hashtable maps GameObject InstanceID to Handler. now raycaster finds that a GameObject has been hit, it looks into hashtable if there’s hadler for this game object if there is then calls OnTouch on handler.

Hope this helps.