Hello everyone, I’m currently having an issue with Unity and it’s buttons.
I am procedurally instantiating buttons on a page in correspondence with the amount of files in a save directory, problem is that I need a way for the player so tell the game which save they want to load.
The Issue lies when I try to add a Listener to each button that is instantiated.
if (Mode == "Load")
{
while (l < 8)
{
//Get the ContentHolder's Position
Vector3 ContentHolderPos = ContentHolder.transform.position;
//Instantiate the Button and assign a new name, instanceID and a random set of 4 numbers.
string k = Instantiate(TextPrefab, new Vector3(142.95598f, listlength + 308, 0), Quaternion.Euler(0, 0, 0)).name = "TextPrefab" + GetInstanceID() + Random.Range(0,1000);
//Set the Parent
GameObject.Find(k).transform.parent = ContentHolder.transform;
//Set the Scale
GameObject.Find(k).transform.localScale = new Vector3(1,1,1);
//GameObject.Find(k).transform.GetChild(0).GetComponent<Button>().onClick.RemoveAllListeners();
//Add the Listener:
GameObject.Find(k).transform.GetChild(0).GetComponent<Button>().onClick.AddListener(() => ButtonCall());
//Log Stuff
Debug.LogError("Tried to add a Listener to " + GameObject.Find(k).name + "'s " + GameObject.Find(k).transform.GetChild(0).name);
//Add to the Listlength Counter
listlength = listlength - 10;
//Add to the Loop Counter
l++;
}
}
}
As you can see, it’s a bit messy, I hope it’s readable.
I’ve marked the section where the button is instantiated then the button is retrieved and the listener is added.
On Play: It makes a series of 8 buttons and does not add a listener to each of them.