AI Pathfinding

Hi

I wrote this code today, but it doesn’t work the way I want it to work.
I want a object to move to one out of four points, and if reached go the another, random one.

The only problem is that if I change the code, it will search a new target every frame, and otherwise the code won’t even search a target.

I tried it with to fix it with some booleans etc. but nothing seems to work…

Does anybody know how to fix this code?

The code so far:

using UnityEngine;
using System.Collections;
using Pathfinding;

public class Citizen_AI : MonoBehaviour {
   
	public GameObject targetObject;
    public GameObject targetObject1;
	public GameObject targetObject2;
	public GameObject targetObject3;
	public GameObject targetObject4;
	[HideInInspector]
	public Vector3 targetPosition;
	public float objectNumber;
	public bool isPicked = false;
    private Seeker seeker;
    private CharacterController controller;
 
    //The calculated path
    public Path path;
    
    //The AI's speed per second
    public float speed = 100;
    
    //The max distance from the AI to a waypoint for it to continue to the next waypoint
    public float nextWaypointDistance = 3;
 
    //The waypoint we are currently moving towards
    private int currentWaypoint = 0;
 
    public void Start () {
        seeker = GetComponent<Seeker>();
        controller = GetComponent<CharacterController>();
        //targetPosition = targetObject.transform.position;
		objectNumber = Random.Range(0, 4);
		Debug.Log("Picked New Number!");
		isPicked = true;
		if(objectNumber == 1){
				targetObject = targetObject1;
		}
		if(objectNumber == 2){
				targetObject = targetObject2;
		} 
		if(objectNumber == 3){
				targetObject = targetObject3;
		}
		if(objectNumber == 4){
				targetObject = targetObject4;
		} 
		targetPosition = targetObject.transform.position;
        //Start a new path to the targetPosition, return the result to the OnPathComplete function
        seeker.StartPath (transform.position,targetPosition, OnPathComplete);
    }
    
    public void OnPathComplete (Path p) {
        Debug.Log ("Yey, we got a path back. Did it have an error? "+p.error);
        if (!p.error) {
            path = p;
            //Reset the waypoint counter
            currentWaypoint = 0;
        }
    }
 
    public void Update () {
        if (path == null) {
            //We have no path to move after yet
            return;
        }
        
        if (currentWaypoint >= path.vectorPath.Count) {
			isPicked = false;
			if(isPicked = false){
    		PickRandomNumber();
			isPicked = true;
			}
            return;
        }
        
        //Direction to the next waypoint
        Vector3 dir = (path.vectorPath[currentWaypoint]-transform.position).normalized;
        dir *= speed * Time.fixedDeltaTime;
        controller.SimpleMove (dir);
        
        //Check if we are close enough to the next waypoint
        //If we are, proceed to follow the next waypoint
        if (Vector3.Distance (transform.position,path.vectorPath[currentWaypoint]) < nextWaypointDistance) {
            currentWaypoint++;
            return;
        }
    }
	
	 public void PickRandomNumber () {
		objectNumber = Random.Range(0, 3);
		//while (  objectNumber != objectNumber ) objectNumber = Random.Range(0, 3);
		Debug.Log("Picked New Number!");
			if(objectNumber == 0){
				targetObject = targetObject1;
		}
			if(objectNumber == 1){
				targetObject = targetObject2;
		} 
			if(objectNumber == 2){
				targetObject = targetObject3;
		}
			if(objectNumber == 3){
				targetObject = targetObject4;
		} 
		targetPosition = targetObject.transform.position;
        //Start a new path to the targetPosition, return the result to the OnPathComplete function
        seeker.StartPath (transform.position,targetPosition, OnPathComplete);
	}
}

//Example.CS

//Example.CS
    
using UnityEngine;
using System.Collections;

public class Example : MonoBehaviour 
{
	GameObject[] points;
	int index;
	float speed = 5.0f;
	
	void Awake()
	{
		//Assuming each point has Tag "WayPoint".
		points = GameObject.FindGameObjectsWithTag("WayPoint");
		index = Random.Range (0, points.Length - 1);
		print (points.Length);
	}
	
	void Update()
	{
		transform.position = Vector3.MoveTowards(transform.position, points[index].transform.position, speed * Time.deltaTime);
		if(transform.position.Equals(points[index].transform.position))
			index = Random.Range(0, points.Length);
	}
}