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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

ArgumentOutOfRangeException - Cannot Find

Discussion in 'Scripting' started by Nigey, Feb 2, 2016.

  1. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Hi Guys,

    I think the fact I can't find WHERE I'm hitting an out of range exception means I need to have a look at the way I was going to do this (I was actually thinking of posting up for how to do this a cleaner way. So any thoughts, shout out). However basically I can't find where the out of range exception is hitting:

    Code (CSharp):
    1.  
    2.         public Point[] GetAllPoints()
    3.         {
    4.             List<Point> pointsList = new List<Point>();
    5.             if (_contents != null && _contents.Count > 0)
    6.             {
    7.                 for (int iChapter = 0; iChapter < _contents.Count; iChapter++)
    8.                 {
    9.  
    10.                     if (_contents[iChapter].sectionsList != null && _contents[iChapter].sectionsList.Count > 0)
    11.                     {
    12.  
    13.                         for (int iSection = 0; iChapter < _contents[iChapter].sectionsList.Count; iSection++)
    14.                         {
    15.  
    16.                             if (_contents[iChapter].sectionsList[iSection].pointList != null && _contents[iChapter].sectionsList[iSection].pointList.Count > 0)
    17.                             {
    18.  
    19.                                 for (int iPoint = 0; iPoint < _contents[iChapter].sectionsList[iSection].pointList.Count; iPoint++)
    20.                                 {
    21.                                     pointsList.Add(_contents[iChapter].sectionsList[iSection].pointList[iPoint]);
    22.                                 }
    23.                             }
    24.                         }
    25.                     }
    26.                 }
    27.             }
    28.             else
    29.             {
    30.                 Debug.Log("Syllabus::GetNextIncomplete() - _contents does not exist, or is empty! Count is " + _contents.Count);
    31.             }
    32.  
    33.             Debug.Log("DOESN'T REACH HERE");
    34.             return pointsList.ToArray();
    35.         }
    36.  
    I'm using Debug.Log's and breakpoints. I can see the breakpoints stop (where it shows in the comments). I just don't have a clue which bit it's referring to.

    _contents contains a list of Chapters. Each chapter contains a list of Sections. Each Section contains a list of Points. I want to return the total number of points in the contents. I can see where the the code breaks, however I don't know from which array it's crashing. Can anyone else see this?
     
    Last edited: Feb 2, 2016
  2. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Code (CSharp):
    1. for (int iSection = 0; iChapter < _contents[iChapter].sectionsList.Count; iSection++)
    2.                         {
    3. }
    Should be iSection < _contents[iChapter].sectionsList.Count
     
    Nigey likes this.
  3. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Well that was quick lol. Thanks. Now for optimizing and finding a better way.
     
  4. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Well you already know how many sections a chapter has and each section knows how many points they have. So why not keep an internal counter? Then you have a function in your "Chapter" which just iterates through it's sections and retrieves that counter for each section? There are many ways you can optimise that code.
     
  5. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    I think that'll be my next step
     
  6. Deleted User

    Deleted User

    Guest

    I may have ideas on how to write it cleaner but I'm still trying to wrap my head around what you are doing here. You are searching the content of each chapter and adding chapters and sections that is being pointed to?
     
  7. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Kinda. I have Chapters, which contain Sections, which contain Points. Basically think of a coursework syllabus, and how the contents page would look. I'm programming something like that. The only reason the Chapters/Sections/Points aren't structs is because they have handy methods in them. These all are brought into a container class, called Syllabus. This is transferred from and to JSON in a server. Normally you don't add any Chapters or anything client side, so it's just doing lookup and syncing them with GameObject's which create interactivity... To do this I need to be able to read/write every Point in the Syllabus.
     
  8. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    FYI: Structs can have functions ;)
     
  9. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Doh! So what is the difference in that case? I know there's something to do with memory and them going onto a different stack or something.
     
  10. Deleted User

    Deleted User

    Guest

    Structs are placed on the stack while classes are placed on the Heap.

    With the stack, the computer reads everything in order from 0-whatever. When things are placed on the stack it is automatically assigned a pointer pointing to the stack resulting in much faster read times.

    With the heap, the computer just sorta plays "go fish". Classes, methods etc are just thrown wherever and the computer grabs the pointer value of each of these and pulls them individually as needed leading to slower read times due to having to manually grab everything.

    Stack is literally just points to the stack of crap you have assigned and starts reading it all one at a time.

    However the Stack has a memory limit which is usually around 1mb but it varies. If you went over your stack limit you will have something called a StackerOverflow error. Things like static variables are also assigned on the stack.

    Structs are faster in this sense but you run into issues if you are using Structs to assign large amounts of data which could start cluttering the Stack as a whole.
     
    Nigey likes this.
  11. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Thanks for the explanation <Level_Up>. Well in that case I guess that'll come down to some testing, but the main thing is optimizing. It might be worth me cracking away at this for a while and then posting something up sometime in the future and asking for some peer reviewing :).
     
  12. Deleted User

    Deleted User

    Guest

    There are other little differences between a struct and a class but that is the major difference.

    As for your code:

    _contents[iChapter].sectionsList is a property with a getter and setter right? You could consider making a bool based property to clean up your if statements and overuse of loops here:

    Code (CSharp):
    1.     // add this to your contents class and rename the variables as they appear in your class
    2.     public bool AddToList
    3.     {
    4.         get
    5.         {
    6.             if(_sectionList.Count == 0 || _pointList.Count == 0)
    7.             { return false; }
    8.             if(_sectionList == null || _pointList == null)
    9.             { return false; }
    10.             return true;
    11.         }
    12.     }
    13.  
    14.  
    15.     public Point[] GetAllPoints()
    16.             {
    17.                 List<Point> pointsList = new List<Point>();
    18.                 if(_contents == null || _conents.Count == 0)
    19.                 {
    20.                     return pointsList.ToArray();
    21.                 }
    22.                 int counter = 0;
    23.                 foreach(Content c in _contents)
    24.                 {
    25.                     if(c.AddToList)
    26.                     {
    27.                         pointsList.Add(c.sectionList[counter].pointList[counter]);
    28.                     }
    29.                
    30.                     counter++;      
    31.                 }
    32.                 return pointsList.ToArray();
    33.             }
    34.  
     
    Last edited by a moderator: Feb 2, 2016
  13. Deleted User

    Deleted User

    Guest

    I edited my previous example because I forgot to change the array element values.

    I'm REALLY shooting blanks here. I am assuming that you are only adding the pointList to your array and that is how I wrote this example. I also assumed that the contents list, section list and pointlist are all pointing to the SAME list element number when you are adding them to the array.

    if my assumptions were correct, then this change I made will work perfectly.