Unity : AI [Navmesh implementation (Part IV)]

NOTE : Even if we talk about IA and NavMesh, I’m take one moment to explain a little thing about collections in programming. Because I have see a lot of people have problems with this logic, (me too at the beginning!) and at the end is more easy than you think.

The point is this : every time we use a collection (Array, List, etc) we have to remember one simple thing : the first element is at 0 INDEX.

Now, .Length or .Count, returns how many elements are in the collection.
It means that if we have an Array of 5 elements,
.Length RETURNS 5, NOT 4.

And in this case, for example, _currentPoint represent the INDEX.

So when we arrive at the end and check if :

_currentPoint == _points.Count,
_currentPoint is 5 BUT _point.Count is 6
and 6 DOES NOT EXIST, because we have only 5 indexs.
And that’s why we have this error -> OUT OF THE BOUND OF THE LIST<>.

To fix this error, we have to check if _currentPoint (index 5) is equal to 5, not 6.
And we know that 5 is 6 - 1

And at the end we move to _currentPoint.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Matteo Lo Piccolo

Matteo Lo Piccolo

82 Followers

Always in love with programming, even if late (I'm already 39 years old) I decided to follow my dream! We will see how far my passion will take me!