Search Unity

Why do i get ArgumentOutOfRangeException?

Discussion in 'Scripting' started by Casper_Stougaard, Jul 25, 2014.

  1. Casper_Stougaard

    Casper_Stougaard

    Joined:
    Jul 18, 2013
    Posts:
    60
    I'm working on a quest system. Everything is working exactly as intended, and all the funtionality is perfect.
    The problem is happening when i have multiple quests at the same time in my quest tracker.
    Udklip.PNG
    If i comlplete the quest in the blue ring, it works perfectly because its the last one in my list.

    But if i complete one of the above ones, like the red ring, i'll get this exception:
    Could someone help me understand this?

    here's the code in which the error is happening:
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var gold : int;
    4. var exp : int;
    5.  
    6. var acceptedQuests : List.<Quest> = new List.<Quest>();
    7. var completedQuests : List.<Quest> = new List.<Quest>();
    8. //var removeQuests : List.<Quest> = new List.<Quest>();
    9.  
    10.  
    11. function Start ()
    12. {
    13.  
    14. }
    15.  
    16. function Update ()
    17. {
    18.  
    19. }
    20.  
    21. function OnGUI ()
    22. {
    23.     GUI.Label (Rect (Screen.width / 2 - 100, 10, 200, 30), "Exp: " + exp);
    24.     GUI.Label (Rect (Screen.width / 2 - 100, 40, 200, 30), "Gold: " + gold);
    25.  
    26.     GUI.Box (Rect (Screen.width - 210,10,200,250), "Quest Tracker");
    27.  
    28.     var i : int = 0;
    29.     for (q in new List.<Quest>(acceptedQuests))
    30.     {
    31.         var buttonText : String;
    32.      
    33.         if (acceptedQuests[i].finished == false) // This is where the error is happening.
    34.         {
    35.             buttonText = acceptedQuests[i].name + " = " + acceptedQuests[i].objectiveCount + " / " + acceptedQuests[i].objectiveReq;
    36.         }
    37.         else if (acceptedQuests[i].finished == true)
    38.         {
    39.             buttonText = "Complete " + acceptedQuests[i].name;
    40.         }
    41.      
    42.         var buttonPosY : int = 40 + (25 * i);
    43.         if (acceptedQuests[i].showInTracker)
    44.         {
    45.             if (GUI.Button (Rect (Screen.width - 205, buttonPosY, 190, 20), buttonText))
    46.             {
    47.                 if (acceptedQuests[i].finished == false)
    48.                 {
    49.                     acceptedQuests[i].GainObjective(1);
    50.      
    51.                     Debug.Log ("Objective increased");
    52.      
    53.                     if (acceptedQuests[i].objectiveCount >= acceptedQuests[i].objectiveReq)
    54.                     {
    55.                         acceptedQuests[i].Finish();
    56.                     }
    57.                 }
    58.                 else if (acceptedQuests[i].finished == true)
    59.                 {
    60.                     exp += acceptedQuests[i].rewardExp;
    61.                     gold += acceptedQuests[i].rewardGold;
    62.                     // Add other rewards here.
    63.          
    64.                     acceptedQuests[i].Complete();
    65.                     completedQuests.Add(acceptedQuests[i]);
    66.                     acceptedQuests.RemoveAt(i);
    67.                 }
    68.             }
    69.         }
    70.         i++;
    71.     }
    72. }
    The "acceptedQuests" list is filled by a questGiver script. As i click the quest giver and accept a quest, it moves the first quest in the questGivers list, into the end of the "acceptesQuests" list in the script above.
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,411
    Just a quick guess,
    since you are removing item there,
    maybe you need to put i--; there after the removeAt() line.. ?
     
    Casper_Stougaard likes this.
  3. Casper_Stougaard

    Casper_Stougaard

    Joined:
    Jul 18, 2013
    Posts:
    60
    Perfect. And the solution makes the explanation to problem obvious. Thank you very much :)