Hello.
I am creating a custom List and when adding an item I instantiate a Button and adding a Listener to that button.
Here is my code
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine.Events;
[System.Serializable]
public class Foods
{
public string name;
public float protein;
public float carbs;
public Foods(string newName, float newProtein,float newCarbs)
{
name = newName;
protein = newProtein;
carbs=newCarbs;
}
}
public class List : MonoBehaviour
{
public static List listRef;
public string name;
public float protein;
public float carbs;
GameObject foodButton;
GameObject deleteFoodButtton;
public GameObject parentPanel;
public GameObject foodBtnPrefab,deleteFoodBtnPrefab;
public Text textToBeChanged,textToGet;
int i=0;
public List<Foods> foodList = new List<Foods>();
void Awake ()
{
if (listRef == null)
{
DontDestroyOnLoad(gameObject);
listRef = this;
}
else if (listRef != this)
{
Destroy(gameObject);
}
}
public void AddFood()
{
foodList.Add(new Foods(name,protein,carbs));
int index=i;
foodButton = (GameObject) Instantiate(foodBtnPrefab);
deleteFoodButtton = (GameObject) Instantiate(deleteFoodBtnPrefab);
foodButton.GetComponentInChildren<Text>().text=foodList[i].name;
foodButton.transform.SetParent(parentPanel.transform,false);
deleteFoodButtton.transform.SetParent(foodButton.transform,false);
deleteFoodButtton.GetComponent<Button>().onClick.AddListener( ()=>{DeleteFood(index);} );
//deleteFoodButtton.GetComponent<Button>().onClick.AddListener(DeleteFood(index));
i++;
}
void DeleteFood( int index)
{
foodList.Remove(foodList[index]);
i--;
}
}
It looks like you are creating a foodButton and deleteFoodButton each time you create an instance of your foods class.
Why not just store references to the buttons in the foods class, and then you can get the relevant buttons from the foodlist items whenever you need to.
I am adding “Foods” to my “foodList” and at the same time I am creating different “foodButton” and “deleteFoodButton” for the food I added to do different things.
If those buttons are directly related to the new food item you created and are what you mean by ‘specificfoodButtons’ in your opening post, then my suggestion should do what’s needed, by enabling you to get a reference to the buttons that were created at the same time as the food item, from the food item itself.
If that’s not what you need, then I’ve misunderstood your problem.
No, I’m suggesting that you store a reference to them in your foods class.
Change your foods class to this
[System.Serializable]
public class Foods
{
public string name;
public float protein;
public float carbs;
public GameObject foodButton;
public GameObject deleteButton;
public Foods(string newName, float newProtein,float newCarbs)
{
name = newName;
protein = newProtein;
carbs=newCarbs;
}
}
Then assign the buttons to the variables like so
public void AddFood()
{
var newFoods = new Foods(name,protein,carbs);
foodList.Add(newFoods);
int index=i;
foodButton = (GameObject) Instantiate(foodBtnPrefab);
deleteFoodButtton = (GameObject) Instantiate(deleteFoodBtnPrefab);
foodButton.GetComponentInChildren<Text>().text=foodList[i].name;
foodButton.transform.SetParent(parentPanel.transform,false);
deleteFoodButtton.transform.SetParent(foodButton.transform,false);
deleteFoodButtton.GetComponent<Button>().onClick.AddListener( ()=>{DeleteFood(index);} );
//deleteFoodButtton.GetComponent<Button>().onClick.AddListener(DeleteFood(index));
i++;
newFoods.foodButton = foodButton;
newFoods.deleteButton = deleteFoodButton;
}
Then you can access the buttons for each foods item via the public variables for the buttons.
void DeleteFood( int index)
{
var button = foodList[index].deleteButton.GetComponent<Button>();
// Do something with button
Debug.Log(button.name);
foodList.Remove(foodList[index]);
i--;
}
It works perfectly ! But I have another problem. Because I am assigning the listenener during button’s creation ,when I remove an item from the middle of the foodList causes index out of range to the other buttons because their index has changed. Is there a workaround ?