ArgumentOutOfRangeException (141827)

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*.golemName, wand);*

});
And i get an ArgumentOutOfRangeException at:
summonGolem(Mod.golems*.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 Mod.golems array ? Have you check the value index ? (>= 0 but < Mod.golems*.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.

1 Answer

1

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*.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.

Never mind i fixed it. Thanks for the help.