Index Out of Range

Hi! When validating HasAnyUnitOnGridPosition, I began to get an Index OutOfRange Exception. I am not sure what I’m missing here… I apologize if I’ve missed an important detail.

What code do you have around GridSystem line 74?
Perhaps you’re doing a for cycle and you did <= max instead of < max

Thanks for getting back so quickly!

Here are the relevant snippets of code. This issue only occurred when testing for the HasAnyUnit validation.



Ok those functions are only getting grid positions, if you give them an invalid position then it should cause an error.
Look in the MoveAction line 74, how are you validating the grid position? You should be testing if it’s inside the bounds before asking the LevelGrid if there’s something on that position.

1 Like

I believe I am doing that. I watched back through the videos again/compared code and still couldn’t find the discrepancy. Here is a broader look at the MoveAction script in that section.

I apologize if I am misunderstanding.

@cmcdow
your first if statement condition inside the loop is incorrect. It should be inverted, ie

if(!LevelGrid.Instance.IsValidGridPosition(testGridPosition))

Notice the ! at the start, which will ensure that if the position is not valid, it bails. Right now, your code is allowing an invalid position to go through likely resulting in your out of bounds exception (assuming that you are getting the error when clicking outside of the grid bounds)

3 Likes

Yup just like @PauloMiguel said, you’re currently ignoring all the valid positions instead of ignoring the invalid position.

This was it exactly. I’m unsure how I got further along in the process without this error not showing up, but I am grateful for all the help! Thank you @PauloMiguel as well!

I am confounded. After applying this fix, a strange error has shown up (or at least I never noticed this as an issue before). Now the grids do not clear when my unit leaves.


I have no idea why this would occur. I went back to the earlier Level Grid video and confirmed that I should have the same code. My Unit script does call the ‘UnitMovedPosition’ method in the Update, and the RemoveUnitAtGridPostion is a part of that method. I’m sorry but I am sure I’m stumbling over a small error again.


On RemoveUnitAtGridPosition you are calling gridObject.RemoveUnit(null); instead of gridObject.RemoveUnit(unit);
That looks like the issue, doesn’t look related to the Move validation at all.

1 Like

Thank you! That was the fix. I am doing this one one screen, looking back and forth. I feel like Im consisting scrambling to catch up. I’m being sure to stop and leave comments as an explanation to myself, but as things are refactored, I think I am missing small bits and rearranging comments. Thanks for the help and the patience!

1 Like

Maybe try watching each lecture twice. First just look at it and try to understand what is being built in that lecture, then watch it again but this time follow it. It’s easier to follow if you first see how everything connects in the end.

1 Like

have you tried using a popout window for the video

I have. My work space is kind small and switching back and forth (even with a popout) is a little difficult. However, I just got a new TV like 3 days ago so now I have the capacity to display my 2nd desktop to it, so I think I have found a viable solution. Thanks for the tip though!

1 Like

I’ve gotten so used to my 2 monitor layout that I couldn’t even imagine NOT working with 2 monitors when coding or following lectures.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms