After the first run of the script (that is, after the sheet is cleared), I get ArgumentOutOfRange In the line newAchiveNumber = achiveList [currentAchive]; I sincerely do not understand why - the situation is completely equivalent and, in theory, the script should not be executed at all (after all, after the idea. List.Clear () List.Count should be = 0, but for some reason it is not reset even if I do List = null

void WaitLine() { if (achiveList!=null&& achiveList.Count !=0 && noOneInWaitLine == false) { Debug.Log(achiveList.Count); newAchiveNumber = achiveList[currentAchive]; if (showFinished == true) { if (curTime == 0) { curTime = Time.time; // Pause over the Waypoint } if ((Time.time - curTime) >= 3f && achiveList.Count != currentAchive) { currentAchive++; curTime = 0; Debug.Log("Timer Finished"); showFinished = false; } } else { ShowAchiv(newAchiveNumber); showFinished = true; if (currentAchive == achiveList.Count) { achiveList.Clear(); currentAchive = 0; noOneInWaitLine = true; } } } } 

    1 answer 1

    Line execution

     newAchiveNumber = achiveList[currentAchive]; 

    it is not protected from the case when currentAchive is equal to or greater than achiveList.Count , which probably occurs after the next increase in currentAchive :

     if ((Time.time - curTime) >= 3f && achiveList.Count != currentAchive) { currentAchive++; curTime = 0; Debug.Log("Timer Finished"); showFinished = false; } 

    You maybe think that block

     if (currentAchive == achiveList.Count) { achiveList.Clear(); currentAchive = 0; noOneInWaitLine = true; } 

    it will catch, but it will not be executed in that WaitLine call, in which currentAchive will reach the value of achiveList.Count , and in the next call - an error will occur.

    Update

    And shouldn't achiveList.Count become 0 after achiveList.Clear ();

    Must, of course. Here you have a working option. Analyze how it differs from yours.

     void WaitLine() { if (achiveList != null && achiveList.Count != 0 && !noOneInWaitLine) { Debug.Log(achiveList.Count); newAchiveNumber = achiveList[currentAchive]; if (showFinished) { if (curTime == 0) { curTime = Time.time; // Pause over the Waypoint } if ((Time.time - curTime) >= 3f && achiveList.Count != currentAchive) { currentAchive++; curTime = 0; Debug.Log("Timer Finished"); showFinished = false; } } else { ShowAchiv(newAchiveNumber); showFinished = true; } if (currentAchive == achiveList.Count) { achiveList.Clear(); currentAchive = 0; noOneInWaitLine = true; } } } 
    • I tried to enter a check if currentAchive <= achiveList.Count) did not help unfortunately - astion
    • And shouldn't achiveList.Count become 0 after achiveList.Clear (); - astion