Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

(SOLVED)Argument Out or Range

Discussion in 'Scripting' started by kingbas33, Sep 11, 2014.

  1. kingbas33

    kingbas33

    Joined:
    Sep 9, 2014
    Posts:
    9
    Why am i getting a ArgumentOutOfRange Exception at line 12?
    Code (CSharp):
    1.     private bool AreRequiredMasteriesUnlocked() { //Checks if required masteries have points added to them
    2.         for (int i = 0; i < requiredMasteries.Count; i++) {
    3.             int temp = i+=1
    4.             if (requiredMasteries.Count > temp) {
    5.                 if (requiredMasteries[i].PointsAdded == true) {
    6.                     continue;
    7.                 }
    8.                 else {
    9.                     return false;
    10.                 }
    11.             }
    12.             else if (requiredMasteries[i].PointsAdded == true) {
    13.                 return true;
    14.             }
    15.  
    16.             else {
    17.                 return false;
    18.             }
    19.         }
    20.         return true;
    21.     }
    22.    
     
  2. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    238
    Might want to try with line 3: += will add 1 to i, but also assign it to i, so i will be i + 1 every time you reach lines 5 or 12.
    Hope this helps.

    ~
    Another walk in the park.
     
  3. DarkArts-Studios

    DarkArts-Studios

    Joined:
    May 2, 2013
    Posts:
    389
    Hi @kingbas33 , @TwixEmma has already provided insight into what could be going wrong.

    However I'd like to suggest a code simplification here, I've read through your code and from what I can tell you're wanting to return true only if all "requiredMasteries.PointsAdded" are true.

    If that is the case, this might provide for a simpler solution:

    Code (CSharp):
    1. private bool AreRequiredMasteriesUnlocked()
    2. {
    3.     foreach ( Mastery mastery in requiredMasteries)
    4.         if ( !mastery.PointsAdded ) return false;
    5.     return true;
    6. }
     
  4. kingbas33

    kingbas33

    Joined:
    Sep 9, 2014
    Posts:
    9
    Thanks for both of your help, @TwixEmma this solved my problem but what @DarkArts Studios suggested makes my code shorter so im using this now.
     
    DarkArts-Studios likes this.
  5. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    238
    You're welcome, and I'm glad you were able to simplify your code! :)

    ~
    Another walk in the park.
     
    DarkArts-Studios likes this.