- Home /
ArgumentOutOfRangeException
I am adding an onClick listener to a button thats created at runtime at runtime using:
currentButton.GetComponent<Button>().onClick.AddListener(delegate {
summonGolem(Mod.golems[i].golemName, wand);
});
And i get an ArgumentOutOfRangeException at:
summonGolem(Mod.golems[i].golemName, wand);
Here is the full error http://pastebin.com/CZ1R4MLB
Please help!
Nobody will be able to help you without more information...
Are you sure you add values to your $$anonymous$$od.golems array ?
Have you check the value index ? (>= 0 but < $$anonymous$$od.golems[i].Length)
^This. You are trying to access a position inside your array that lies outside that array's boundaries, which are between 0 and it's Length -1.
Im using the same index in there as i am when accessing other data from the same array in a different method. And if i hardcode the value it works just fine.
Answer by Mark Gossage · Jun 23, 2015 at 12:18 PM
Your value for i is incorrect & you are running off the end of the golems array. Add in some debug code like this:
currentButton.GetComponent<Button>().onClick.AddListener(delegate { summon(i);});
void summon(int i)
{
Debug.Log("summon "+i);
Debug.Log("#Golems "+Mod.golems.length);
summonGolem(Mod.golems[i].golem,wand);
}
As your error is in the delegate its very hard to add debug code. This way its easier to add the debug code & find out why it went wrong.
Thank you because of that i found why it didnt work... for some reason it added 1 to i.
Ok and i just tested the other buttons and they all use the same index. How can i fix this?
Can you add the rest of the code to the question, in particular the part that sets the value for i. That will give me a better idea on why they are using the same value for i.
Your answer
![](https://koobas.hobune.stream/wayback/20220613201302im_/https://answers.unity.com/themes/thub/images/avi.jpg)