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
.
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++)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.