public SkeletonWaypoint NextWaypoint(SkeletonWaypoint previousWaypoint)
    {
        if (neighbours.Count == 0)
        {
            Debug.LogError("Need more waypoints");
            return(null);
        }

        else if (neighbours.Count == 1 && neighbours.Contains(previousWaypoint))
        {
            return(previousWaypoint);
        }

        else
        {
            SkeletonWaypoint nextWaypoint;

            int nextIndex = 0;

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

            return(nextWaypoint);
        }
    }
    private void SetTarget()
    {
        if (waypointsVisited > 0)
        {
            SkeletonWaypoint nextTarget = currentTarget.NextWaypoint(prevTarget);
            prevTarget    = currentTarget;
            currentTarget = nextTarget;
        }

        Vector3 targetVector = currentTarget.transform.position;

        navMeshAgent.SetDestination(targetVector);
        inMotion = true;
        if (animator != null)
        {
            animator.SetBool("isWalking", true);
            animator.SetBool("isIdle", false);
        }
    }
    public void Start()
    {
        // Find all skeleton waypoints in the scene
        GameObject[] allWaypoints = GameObject.FindGameObjectsWithTag("SkeletonWaypoint");

        neighbours = new List <SkeletonWaypoint>();

        // Check if waypoints are close enough
        for (int w = 0; w < allWaypoints.Length; w++)
        {
            SkeletonWaypoint nextWaypoint = allWaypoints[w].GetComponent <SkeletonWaypoint>();

            if (nextWaypoint != null)
            {
                if (Vector3.Distance(this.transform.position, nextWaypoint.transform.position) <= proximityRadius && nextWaypoint != this)
                {
                    neighbours.Add(nextWaypoint);
                }
            }
        }
    }
    void Start()
    {
        navMeshAgent = this.GetComponent <NavMeshAgent>();
        animator     = this.GetComponent <Animator>();

        if (navMeshAgent == null)
        {
            Debug.LogError(gameObject.name + " is missing NavMesh");
        }
        else
        {
            if (currentTarget == null)
            {
                // Find all skeleton waypoints in the scene
                GameObject[] allWaypoints = GameObject.FindGameObjectsWithTag("SkeletonWaypoint");

                if (allWaypoints.Length > 0)
                {
                    while (currentTarget == null)
                    {
                        int firstIndex = UnityEngine.Random.Range(0, allWaypoints.Length);
                        SkeletonWaypoint startingWaypoint = allWaypoints[firstIndex].GetComponent <SkeletonWaypoint>();


                        if (startingWaypoint != null)
                        {
                            currentTarget = startingWaypoint;
                        }
                    }
                }
                else
                {
                    Debug.LogError("No waypoints in the scene");
                }
            }
            SetTarget();
        }
    }