Reputation: 265
Here is my code:
ShopButton[] allButtons = FindObjectsOfType<ShopButton> ();
for (int i = 0; i < allButtons.Length; i++)
{
allButtons [i].UpdateButtonState ((GameDataManager.publicInstance.skinAvailability & 1 << allButtons [i - 1].ninjaNumber) == 1 << allButtons [i - 1].ninjaNumber);
}
When I run it this code gives me an IndexOutOfRangeException
.
Upvotes: 0
Views: 661
Reputation: 1125
I think the reason is because of the first index that causes a problem.
if i starts with 1 then i-1 will be 0 and therefore the index 0 exists in array allbuttons.
Clearly : if starting i is 0
, then i-1
will be -1
; this will cause index out of range exception
because all arrays start with index 0
and have no index -1. ;
Starting with i = 1
the error will be solved because then i-1
will be 0
which is in range of indexes of any array.
change part of loop to :
for (int i = 1; i < allButtons.Length; i++)
Upvotes: 2