- Home /
How do get a specific item in a list?
Hi. I have a list of buttons and i want to get the one i clicked.
[System.Serializable]
public class Item
{
public string name;
public Sprite icon;
public int cps;
public int cost;
public Button.ButtonClickedEvent OnClickShopButton;
public bool bought = false;
}
public class CreateScrollList : MonoBehaviour {
public GameObject sampleButton;
public List<Item> itemList;
public Transform contentPanel;
void Start () {
PopulateList();
}
void PopulateList() {
foreach (var item in itemList)
{
GameObject newButton = Instantiate(sampleButton) as GameObject;
SampleButton button = newButton.GetComponent<SampleButton>();
button.nameLabel.text = item.name;
button.iconImage.sprite = item.icon;
button.CPS.text = "CPS: " + item.cps;
button.Cost.text = "Cost: " + item.cost;
button.button.onClick = item.OnClickShopButton;
newButton.transform.SetParent(contentPanel);
}
}
public void OnClickShopButton()
{
Item item = // What should i type here to get the item i clicked
if(CakeManager.cakes >= item.cost){
item.bought = true;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
List to buttons 1 Answer
How to make a deep copy instead of a shallow one? 1 Answer
Multiple Cars not working 1 Answer
Add item to string by name? 1 Answer