public ConnectedWaypoint NextWaypoint(ConnectedWaypoint previousWaypoint)
        {
            if (connections.Count == 0)
            {
                //No waypoints?  Return null and complain.
                Debug.LogError("Insufficient waypoint count.", gameObject);
                return(null);
            }
            else if (connections.Count == 1 && connections.Contains(previousWaypoint))
            {
                //Only one waypoint and it's the previous one?  Just use that.
                return(previousWaypoint);
            }
            else
            {
                ConnectedWaypoint nextWaypoint;
                int nextIndex = 0;

                do
                {
                    nextIndex    = UnityEngine.Random.Range(0, connections.Count);
                    nextWaypoint = connections[nextIndex];
                } while (nextWaypoint == previousWaypoint);

                return(nextWaypoint);
            }
        }
        private void SetDestination()
        {
            if (waypointsVisited > 0)
            {
                ConnectedWaypoint nextWaypoint = currentWaypoint.NextWaypoint(previousWaypoint);
                previousWaypoint = currentWaypoint;
                currentWaypoint  = nextWaypoint;
            }

            Vector3 targetVector = currentWaypoint.transform.position;

            navMeshAgent.SetDestination(targetVector);
            traveling = true;
        }
        private void Start()
        {
            navMeshAgent = this.GetComponent <NavMeshAgent>();

            if (navMeshAgent == null)
            {
                Debug.LogError("The nav mesh agent component is not attached to " + name, gameObject);
            }
            else
            {
                if (currentWaypoint == null)
                {
                    //Set it at random
                    //Grab all waypoint objects in scene.
                    GameObject[] allWaypoints = GameObject.FindGameObjectsWithTag("Waypoint");

                    if (allWaypoints.Length > 0)
                    {
                        while (currentWaypoint == null)
                        {
                            int random = UnityEngine.Random.Range(0, allWaypoints.Length);
                            ConnectedWaypoint startingWaypoint = allWaypoints[random].GetComponent <ConnectedWaypoint>();

                            //i.e. we found a waypoint.
                            if (startingWaypoint != null)
                            {
                                currentWaypoint = startingWaypoint;
                            }
                        }
                    }
                    else
                    {
                        Debug.LogError("Failed to find any waypoints for use in the scene");
                    }
                }

                SetDestination();
            }
        }
        public void Start()
        {
            //Grab all waypoint objects in scene.
            GameObject[] allWaypoints = GameObject.FindGameObjectsWithTag("Waypoint");

            //Create a list of waypoints I can refer to later.
            connections = new List <ConnectedWaypoint>();

            //Check if they're a connected waypoint.
            for (int i = 0; i < allWaypoints.Length; i++)
            {
                ConnectedWaypoint nextWaypoint = allWaypoints[i].GetComponent <ConnectedWaypoint>();

                //i.e. we found a waypoint
                if (nextWaypoint != null)
                {
                    if (Vector3.Distance(this.transform.position, nextWaypoint.transform.position) <= connectivityRadius && nextWaypoint != this)
                    {
                        connections.Add(nextWaypoint);
                    }
                }
            }
        }