這裏是我的代碼:陣列中的for循環給數組索引超出範圍異常
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);
}
當我運行這段代碼給了我一個IndexOutOfRangeException
。
因爲第一環上,'i'將爲0和'I - 1'將爲-1,這是不適合的陣列的有效折射率。 –
變化來回環路以開始1而不是零:對(INT I = 1;我
jdweng
我已經嘗試i-1和類似@IanH。說i-1導致索引是-1,導致相同的錯誤... – RaZ