Instantiated Buttons to have separate On-click functions
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ButtonListControl : MonoBehaviour {
[SerializeField]
private GameObject ButtonTemplate;
[SerializeField]
private GameObject[] ApplianceList;
GameObject ApplianceGUI;
private int index;
void Start()
{
ApplianceGUI = GameObject.Find("Canvas_GUI_ScrollList");
ApplianceGUI.SetActive(false);
// Finds GUI and turns off on run.
foreach (GameObject Appliance in ApplianceList)
{
GameObject Button = Instantiate(ButtonTemplate) as GameObject;
Button.GetComponent<ButtonListButton>().SetText(Appliance.name);
Button.transform.SetParent(ButtonTemplate.transform.parent, false);
Appliance.SetActive(false);
if (ApplianceList[0])
ApplianceList[0].SetActive(true);
Button.SetActive(true);
}
}
public void Option1Clicked() { foreach (GameObject Appliance in ApplianceList) { Appliance.SetActive(false); //Sets the current Game Object to false
ApplianceList[1].SetActive(true);
Debug.Log("New Appliance Chosen, Hiding Old model and displaying:", ApplianceList[1]);
}
}
public void Option2Clicked()
{
foreach (GameObject Appliance in ApplianceList)
{
Appliance.SetActive(false);
//Sets the current Game Object to false
ApplianceList[2].SetActive(true);
Debug.Log("New Appliance Chosen, Hiding Old model and displaying:", ApplianceList[2]);
}
}
public void Option3Clicked()
{
foreach (GameObject Appliance in ApplianceList)
{
Appliance.SetActive(false);
//Sets the current Game Object to false
ApplianceList[3].SetActive(true);
Debug.Log("New Appliance Chosen, Hiding Old model and displaying:", ApplianceList[3]);
}
}
public void Option4Clicked()
{
foreach (GameObject Appliance in ApplianceList)
{
Appliance.SetActive(false);
//Sets the current Game Object to false
ApplianceList[4].SetActive(true);
Debug.Log("New Appliance Chosen, Hiding Old model and displaying:", ApplianceList[4]);
}
}
Comment