Cannot seem to raise an integer using ++

I am trying to move a patrolling enemy from one patrol point to the next, but for some reason it only goes to the first patrol point in the Transform array, or element 0.

Here is the code for this enemy (under Update()):

                if (shouldPatrol)
                {
                    moveDirection = patrolPoints[currentPatrolPoint].position - transform.position;

                    if(Vector3.Distance(transform.position, patrolPoints[currentPatrolPoint].position) < .1f)
                    {
                        currentPatrolPoint++;
                        if(currentPatrolPoint >= patrolPoints.Length)
                        {
                            currentPatrolPoint = 0;
                        }
                    }
                }

Here’s a screenshot of the enemy stuck in position 0. I turned debug on so you can see the “current patrol point” is stuck on 0.


Put a Debug.Log before currentPatrolPonit++ and I bet you’re not reaching it.

1 Like

Damn you’re so right! But why the hell not? The patrol point doesn’t have a collider on it or anything. Why wouldn’t it ever actually reach that point?

What’s your code for actually moving towards the point?

Could be so many reasons :slight_smile:

  • the point is not on the same plane, ie the y value is much higher then your patrolling enemy
  • the patrol points array is empty or the second point is not assigned to the array

if you post more of the code and a print screen of your setup

So I ended up doing it via OnTriggerEnter2D instead. Turns out I was having a transform.position issue due to the other thread you replied to - where I’m spawing enemies using spawn points instead of traditional placement.