Boolean problem with patrol logic

Hi everyone,

I got a dude wich is a navMeshAgent who’s patrolling between waypoints. He’s moving alright and can reach is waypoint. The problem come that i got a boolean that supposed to start at false and once he reach is destination I got a timer that start and once at zero the agent swicth waypoint.

The problem is that my freaking boolean always start true!!! it’s tied to the distance and i’ve made it public so i can monitor it and it’s really start at 25 and should return true only when it’s reach .2 or less.

Here’s my code :
public class Movement : MonoBehaviour
{
NavMeshAgent agent;
Transform target;
public Transform waypoint1;
public Transform waypoint2;
Animator animator;
public float speed;
public float distance;

	public float timerMax = 500;
	bool reachedDestination = false;

	// Use this for initialization
	void Start () 
	{
		agent = GetComponent<NavMeshAgent>();
		animator = GetComponent<Animator>();
		target = waypoint1.transform;
		reachedDestination = false;
	}
	
	// Update is called once per frame
	void Update () 
	{
		//Debug
		Debug.Log(reachedDestination);

		speed = agent.speed;
		animator.SetFloat("Speed",speed);
		agent.SetDestination(target.position);
		//ReachDestination();

		//Reaching destination
		distance = agent.remainingDistance;
		if(distance <= 0.2)
		{
			agent.speed = 0;
			reachedDestination = true;
		}
		if(distance >= 0.2)
		{
			agent.speed = 1;
		}

		//Timer for next destination///////
		if(reachedDestination)
		{
			timerMax--;
			if(timerMax <=0)
			{
				reachedDestination = false;
				target = waypoint2.transform;
				timerMax = 500;
			}
		}

	}
}

I’m sure it’s a stupid thing but i don’t see my error anywere!

Add a Debug.Log() statement right before you set reachedDestination to true that outputs the distance value. It is possible that the distance is something small very briefly at the start of the game, and is too brief for you to notice in the inspector.

Proper debugging must be done with log statements or breakpoints!