Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

ArgumentOutOfRangeException - Comparing two things...

Discussion in 'Scripting' started by Ovredon, Jan 12, 2014.

  1. Ovredon

    Ovredon

    Joined:
    Jul 13, 2013
    Posts:
    161
    Hello,

    So I have a List of Integers and a List of List Objects.

    Basically I am just trying to compare the two while in a for loop but for some strange reason I get the error;

    "ArgumentOutOfRangeException: Argument is out of range"...

    When I debug the two return values I am receiving numbers, so they can't be null or non-existent ?

    Code (csharp):
    1.  
    2.  
    3. public List<List<Object>> listOfLists= new List<List<Object>>();
    4.  
    5. List<int> intList;
    6.  
    7. ....
    8.  
    9. if(intList.Count == 0)
    10. {
    11.     intList.Add (new int()); // Tried this and something similar with listOfLists to make sure there is definitely content within them. Debug.Log with a ToString() revealed that they do have values.
    12.     intList[i] = 1;
    13. }
    14.  
    15. if(listOfLists.Count != intList[i])     // Error points to this..
    16. {
    17.  
    18. }
    19.  
    20.  
    So yeah. Please if someone could help me I would be thankful.
     
    Last edited: Jan 12, 2014
  2. Zeblote

    Zeblote

    Joined:
    Feb 8, 2013
    Posts:
    1,102
    You'll get that exception if i is >= intList.Count

    What's setting i?
     
  3. Ovredon

    Ovredon

    Joined:
    Jul 13, 2013
    Posts:
    161
    i is part of a for loop. When this first iterates it should be set to 0

    Not to mention I'm comparing the amount of items in a list, returning a integer, and whatever the int is set at in intList[0]
     
  4. MDragon

    MDragon

    Joined:
    Dec 26, 2013
    Posts:
    329
    Sorry if this suggestion doesn't really work, but have you tried i-1? Seeing only so little doesn't give me much room to do much gues- I mean, thinking work :)
     
  5. unitylover

    unitylover

    Joined:
    Jul 6, 2013
    Posts:
    346
    Count is always 1 higher than the key index value in the list.
     
  6. Ovredon

    Ovredon

    Joined:
    Jul 13, 2013
    Posts:
    161
    I'm sure I tried something -1 as I have this error on a few instances on using Count... I'll give it another go thanks - any other suggestions are welcome though as I am a bit doubtful this'll work.
     
  7. Ovredon

    Ovredon

    Joined:
    Jul 13, 2013
    Posts:
    161
    Sorry. I got the initial example slightly wrong but I just amended it.

    Trying the Count - 1 thing didn't work. and for some reason the error sometimes indicates this section too....

    "if(intList.Count == 0)"

    Its such a strange error to me, and the console really doesn't give much to go on.
     
  8. Zeblote

    Zeblote

    Joined:
    Feb 8, 2013
    Posts:
    1,102
    Post the full code
     
  9. MDragon

    MDragon

    Joined:
    Dec 26, 2013
    Posts:
    329
    If you post your full script it'd be much easier to find the source of the error.
     
  10. Ovredon

    Ovredon

    Joined:
    Jul 13, 2013
    Posts:
    161
    Uuugh. This is really fustrating, sometimes it comes up with this error and sometimes it doesn't. I mean seriously, how can Debug.Log(intList.Count.ToString()); create an error ??
     
  11. Zeblote

    Zeblote

    Joined:
    Feb 8, 2013
    Posts:
    1,102
    Post the code!
     
  12. unitylover

    unitylover

    Joined:
    Jul 6, 2013
    Posts:
    346
    We are all dieing to help you but you must post relevant code so we can duplicate the issue on our end.
     
  13. Ovredon

    Ovredon

    Joined:
    Jul 13, 2013
    Posts:
    161
    I'm going to take a completely different approach to this if I cannot figure it out tonight. Thanks for the effort but don't worry about it. I would post my code but its all over the place and the error jumps between sections exactly like what I linked at the start of the post, only it happens in loads of different places across 300 lines of code.

    Thanks anyway.
     
  14. Zeblote

    Zeblote

    Joined:
    Feb 8, 2013
    Posts:
    1,102
    just post the code omg
     
  15. MDragon

    MDragon

    Joined:
    Dec 26, 2013
    Posts:
    329
    I will hold myself back... So... I will point here:

    Another pair of eyes is very helpful. Another bunch of eyes on the internet in a non-intrusive way is also very helpful
     
  16. unitylover

    unitylover

    Joined:
    Jul 6, 2013
    Posts:
    346
    Don't be afraid of posting "spaghetti code" because we've all been there. It's how you learn to code better.
     
  17. Ovredon

    Ovredon

    Joined:
    Jul 13, 2013
    Posts:
    161
    Ok give me a minute.
     
  18. Ovredon

    Ovredon

    Joined:
    Jul 13, 2013
    Posts:
    161
    OKAY

    Thank you for the enthusiasm I appreciate it.

    Here .. Sorry if there is a rogue curly bracket somewhere. I cut out only the relevant code that I'm working on which is giving me an issue. What I'm basically trying to do is be able to dynamically create a Master windows which hold a int each, changing that int will give GameObject Boxes/windwos that you can drag and drop onto.

    So

    MasterWindowAmount = 2 would give you 2 entries in ListOfWindows and the ability to set an int which affects each of those entires during the creation as they both refer to i variable on the for loop.

    Changing those int variables > 0 will create GameObject inspecter boxes... That's the idea anyway.




    Code (csharp):
    1.  
    2.  
    3. // Code removed because it was wrong and buggy
    4. // Don't want someone finding this page and trying to use it and having the pains I had with it ..
    5. // Current working (ish) version still buggy in some places and very messy so will not post yet.
    6.  
    7.  
     
    Last edited: Jan 12, 2014
  19. Ovredon

    Ovredon

    Joined:
    Jul 13, 2013
    Posts:
    161
    if I change != to == on line 100 and I uncomment 127 it works....

    But only on initialize / startup.. if I attempt to change a variable it all goes wrong throwing that error.
     
  20. Ovredon

    Ovredon

    Joined:
    Jul 13, 2013
    Posts:
    161
    Kind of fixed the issue. Still a bit buggy but it mostly works. Will hopefully manage to fully fix it for.

    Don't know quite why it is fixed, just kept tweaking every possible value and variable I could think of. Guess I finally got lucky.

    Thanks for the effort anyway, everyone.