I’ve looked around and even though there were people with similar problems to this one, none of the solutions could help my case.
What I want to do is to create multiple buttons at runtime, all of the buttons will call the same function on another script, but will provide different parameters (gameObjects).
So this is the code:
for (int i = 0; i < cockpits.Count; i++)
{
//Create Buttons
GameObject b = GameObject.Instantiate (buttonPrefab);
b.transform.SetParent (cockPitsRect.transform, false);
b.transform.localScale = new Vector3 (1, 1, 1);
Text t = b.GetComponentInChildren<Text> ();
t.text = cockpits *.name;*
-
Button tempButton = b.GetComponent<Button> ();*
_ tempButton.onClick.AddListener(() => shipScript.AddPart(cockpits*));*_
* RectTransform buttonRect = tempButton.GetComponent ();*
_ b.name = cockpits .name;
* buttonRect.anchoredPosition = new Vector2 (0, 0.5f);
buttonRect.localPosition = new Vector2(cockPitsRect.sizeDelta.x /2 -1 + buttonRect.sizeDelta.x/2 + i100,0);
cockpitButtons.Add (b);*_
* }//For*
And this is the part that is causing trouble:
tempButton.onClick.AddListener(() => shipScript.AddPart(cockpits*));*
I mean, it should be simple, right? I call the function “AddPart” on the “shipScript” script, and provide the parameter which is the gameobject on the list of gameobjects “cockpits”.
But it doesn’t work. The game compiles just fine, but when I try to click on any of the 3 buttons, this appears on the console:
ArgumentOutOfRangeException: Argument is out of range
So I tried debbuging it and turns out that the “i” value, even though it should be 0, 1, and 2 for the first, second and third buttons respectively, becomes “3” when I press the button (that is why it becomes out of range I guess?).
So I really have no idea how can I make this work.
Thanks in advance!