JustPaste.it
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class Inhabitant : MonoBehaviour
{
    // Start is called before the first frame update
    public TreeManager treeManager;
    public string inhabitantState = "idle";
    float timeLeft = 3f;

    Tree lockedTree = null;
    GameObject lockedWarehouse = null;

    public int resourcesGathered = 0;

    public UnityEngine.AI.NavMeshAgent agent;

    void Start ()
    {
        this.agent = GetComponent<NavMeshAgent>();
    }

    // Update is called once per frame
    void Update ()
    {
       
        if (inhabitantState == "idle") {

            GetClosestObject("Tree");

            // If wood is available, switch state to get wood
            if (lockedTree != null){
                //Debug.Log("Switching to movingToWood");
                inhabitantState = "movingToWood";
            }

        }

        if (inhabitantState == "movingToWood"){

            agent.SetDestination(lockedTree.transform.position);

            if ((lockedTree.transform.position - transform.position).magnitude < 5){
               //Debug.Log("Switching to choppingWood");
               inhabitantState = "choppingWood";

               timeLeft = 3f;
            }

        }

        if (inhabitantState == "choppingWood"){

            while (resourcesGathered <= 5){

                lockedTree.StartCuttingTree();

            }

            inhabitantState = "movingToWarehouse";
        }

        if (inhabitantState == "movingToWarehouse"){

            GetClosestObject("Warehouse");

            agent.SetDestination(lockedWarehouse.transform.position);

            if ((lockedWarehouse.transform.position - transform.position).magnitude < 5){
               //Debug.Log("Switching to storingInWarehouse");
               inhabitantState = "storingInWarehouse";

               timeLeft = 3f;
            }

        }

        if (inhabitantState == "storingInWarehouse"){
            timeLeft -= Time.deltaTime;

            if (timeLeft < 0){
                //Debug.Log("idle");
                inhabitantState = "idle";
            }
        }

    }


    // Rewrite to accept an array instead of list!!
    Transform GetClosestObject (string tagName){

        List<Tree> trees = this.treeManager.GetTrees();

        if (tagName == "Tree"){
            //If this list is not empty
            if (trees != null){
   
                Transform bestTarget = null;
                Tree bestTree = null;
                float closestDistanceSqr = Mathf.Infinity;
                Vector3 currentPosition = transform.position;

                foreach(Tree treeObject in trees)
                {
                    Vector3 directionToTarget = treeObject.transform.position - currentPosition;
                    float dSqrToTarget = directionToTarget.sqrMagnitude;
                    if(dSqrToTarget < closestDistanceSqr)
                    {
                        closestDistanceSqr = dSqrToTarget;
                        bestTarget = treeObject.transform;
                        bestTree = treeObject;
                    }
                }

                if (tagName == "Tree"){
                    lockedTree = bestTree;
                }

                return bestTarget;

            }

            else{
               
                if (tagName == "Tree"){
                    lockedTree = null;
                }
               
                return null;
            }
        }

        if (tagName == "Warehouse"){

            // Gets a list of all objects with a certain name and adds it into a Transform list
            GameObject[] allObjects = UnityEngine.Object.FindObjectsOfType<GameObject>();
            List<Transform> objectsWithName = new List<Transform>();

            if (objectsWithName != null){

                Transform bestTarget = null;
                float closestDistanceSqr = Mathf.Infinity;
                Vector3 currentPosition = transform.position;

                //Filter by tagname
                foreach (GameObject obj in allObjects){
                if (obj.tag == tagName){
                        objectsWithName.Add(obj.transform);
                    }
                }

                foreach(Transform obj in objectsWithName)
                {
                    Vector3 directionToTarget = obj.transform.position - currentPosition;
                    float dSqrToTarget = directionToTarget.sqrMagnitude;
                    if(dSqrToTarget < closestDistanceSqr)
                    {
                        closestDistanceSqr = dSqrToTarget;
                        bestTarget = obj.transform;
                    }
                }

                return bestTarget;
            }

            else {
                return null;
            }

        }

        else return null;
    }

    public void SetTreeManager(TreeManager treeManager){
        this.treeManager = treeManager;
    }
}

 

 

 

 

 

 

----------------------------------------------------------

 

 

 

 

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CreatureManager : MonoBehaviour
{

    private TreeManager treeManager;
    private List<Inhabitant> inhabitants = new List<Inhabitant>();

    // Start is called before the first frame update
    void Start()
    {
        // Creates the container and puts a tree in
        GameObject inhabitantObject = Instantiate(Resources.Load<GameObject>("Prefabs/Testing prefabs/Inhabitant"));  //new GameObject("TreeObject");
        inhabitantObject.transform.position = new Vector3(5,1,11);
        //inhabitantObject.transform.rotation = new Quaternion.identity;

        Inhabitant newInhabitant = inhabitantObject.AddComponent<Inhabitant>();
        newInhabitant.SetTreeManager(treeManager);

        inhabitants.Add(newInhabitant);

    }

    // Update is called once per frame
    void Update()
    {
       
    }

    public void SetTreeManager(TreeManager treeManager){
        this.treeManager = treeManager;
    }
}