Scripts dont work properly when there are more than 15 objects

i am having difficulty in running same script on different clones of a single prefab when there are 5 or 6 six gameObject script works correrctly but when there are more then 15 gameobjects all of them do nothing they just going back and forward or doing nothing

first script is attached to worker gameobject and second one is attached to farm gameobject

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

public class WorkInFarm : MonoBehaviour {

    GameObject closestFarm;
    List<GameObject> chechkedFarms;
    Unit unit;
    Scripts scripts;

    int farmNodeCount;
    int currentNodeNumb;
    Node currentNode;
    Farm farm;
    JobSystem jobSystem;

    float farmingTime = 0.5f;
    float farmingTimeLeft;
    float reapingTime = 1.0f;
    float reapingTimeLeft;
    void Start ()
    {
        scripts = GameObject.Find("GM").GetComponent<Scripts>();
        chechkedFarms = new List<GameObject>();
        jobSystem = GetComponent<JobSystem>();
        unit = GetComponent<Unit>();
        farmingTimeLeft = farmingTime;
        reapingTimeLeft = reapingTime;
   
    }
   
    void Update ()
    {
   
    }

    public void startWorkInFarm()
    {
        StopCoroutine("workInFarm");
        StartCoroutine("workInFarm");
    }

    IEnumerator workInFarm()
    {
        while(true)
        {
            if (closestFarm == null)
            {
                findClosestFarm();
            }
            else if (!farm.allGrowing(currentNode))
            {
                Node n = farm.farmNodes[currentNodeNumb];
                if (!farm.occupiedNodes.Contains(n) || currentNode == n)
                {
                    if (!farm.isGrowing(n))
                    {
                        if (!farm.isReadyToReap(n))
                        {
                            if (currentNode != n)
                            {
                                currentNode = n;
                                farm.occupiedNodes.Add(currentNode);
                            }
                            if (transform.position == n.worldPosition)
                            {
                                if (farmingTimeLeft <= 0)
                                {
                                    currentNodeNumb = increaseNodeNumb(currentNodeNumb);
                                    farmingTimeLeft = farmingTime;
                                    farm.occupiedNodes.Remove(currentNode);
                                    farm.addToGrowingNode(currentNode);
                                    currentNode = null;
                                }
                                else
                                {
                                    farmingTimeLeft -= Time.deltaTime;
                                }
                            }
                            else if (!unit.havePath)
                            {
                                unit.setDestination(n.worldPosition);
                            }
                        }
                        else
                        {
                            if (farm.allReadyToReap())
                            {
                                if (!farm.collectedCrops.Contains(n))
                                {
                                    #region reap
                                    if (currentNode != n)
                                    {
                                        currentNode = n;
                                        farm.occupiedNodes.Add(currentNode);
                                    }
                                    if (transform.position == n.worldPosition)
                                    {
                                        if (reapingTimeLeft <= 0)
                                        {
                                            currentNodeNumb = increaseNodeNumb(currentNodeNumb);
                                            reapingTimeLeft = reapingTime;
                                            farm.occupiedNodes.Remove(currentNode);
                                            farm.collectedCrops.Add(currentNode);
                                            farm.growAmountNodes[n] = 0;
                                            farm.visualizeCrops(currentNode);
                                            currentNode = null;
                                        }
                                        else
                                        {
                                            reapingTimeLeft -= Time.deltaTime;
                                        }
                                    }
                                    else if (!unit.havePath)
                                    {
                                        unit.setDestination(n.worldPosition);
                                    }
                                    #endregion
                                }
                                else
                                {
                                    currentNodeNumb = increaseNodeNumb(currentNodeNumb);
                                }

                            }
                            else
                            {
                                currentNodeNumb = increaseNodeNumb(currentNodeNumb);
                            }
                        }
                    }
                    else
                    {
                        currentNodeNumb = increaseNodeNumb(currentNodeNumb);
                    }
                }
                else
                {
                    currentNodeNumb = increaseNodeNumb(currentNodeNumb);
                }
            }
            else
            {
                findClosestFarm();
            }
            yield return null;
        }
      
    }

    public void cancelJob()
    {
        currentNode = null;
        currentNodeNumb = 0;
        farm.workers--;
    }

    void findClosestFarm()
    {
        GameObject[] farms = GameObject.FindGameObjectsWithTag("Farm");
        float dist = Mathf.Infinity;
        GameObject closest = null;
        if(farms.Length > 0)
        {
            foreach(GameObject g in farms)
            {
                Vector3 diff = transform.position - g.transform.position;
                float currentDist = diff.sqrMagnitude;
                if(currentDist < dist && !chechkedFarms.Contains(g))
                {
                    closest = g;
                    dist = currentDist;
                }
            }
        }
        if (closest != null)
        {
            farm = closest.GetComponent<Farm>();
            if(!farm.allGrowing(null) && farm.workers<1)
            {
                closestFarm = closest;
                farm.workers++;
                currentNodeNumb = 0;
                chechkedFarms.Clear();
            }
            else
            {
                chechkedFarms.Add(closest);
                findClosestFarm();
            }
        }
        else
        {
            jobSystem.currentJob = JobTypes.notWorking;
            unit.setDestination(Vector3.zero);
        }
       
    }

    int increaseNodeNumb(int i)
    {
        if (i >= farm.farmNodes.Count-1)
        {
            i = 0;
            return i;
        }
        else
        {
            i++;
        }
        return i;
    }

    void findNextAvailableNode()
    {
        Node n = farm.farmNodes[currentNodeNumb];
        if(!farm.isFull(n) || currentNode != n)
        {
            if(!farm.isGrowing(n))
            {
                currentNode = n;
            }
            else
            {
                currentNodeNumb = increaseNodeNumb(currentNodeNumb);
            }
        }
        else
        {
            currentNodeNumb = increaseNodeNumb(currentNodeNumb);
        }
    }
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Farm : MonoBehaviour {

    public List<Node> farmNodes;
    public List<Node> occupiedNodes;
    public List<Node> growingNodes;
    Dictionary<Node, float> growNodes;
    public Dictionary<Node, int> growAmountNodes;
    public List<Node> readyToReap;
    public List<Node> collectedCrops;
    public int workers;
    Dictionary<Node, GameObject> crops;

    public GameObject grainCube;
    public GameObject readyToHaulCrop;

    public Material firstPhase;
    public Material secondPhase;
    public Material thirdPhase;
    public Material lastPhase;

    float f;
    int next = 0;
    void Start ()
    {
        occupiedNodes = new List<Node>();
        growingNodes = new List<Node>();
        growNodes = new Dictionary<Node, float>();
        growAmountNodes = new Dictionary<Node, int>();
        collectedCrops = new List<Node>();
        crops = new Dictionary<Node, GameObject>();
        readyToReap = new List<Node>();
        f = 0.5f;

        foreach(Node n in farmNodes)
        {
            growAmountNodes.Add(n, 0);
            Vector3 pos = new Vector3(n.worldPosition.x, -1.5f, n.worldPosition.z);
            GameObject g = (GameObject)Instantiate(grainCube, pos, Quaternion.identity);
            crops.Add(n, g);
        }
    }
   
   
    void Update ()
    {
        f -= Time.deltaTime;
        if(growNodes.Count > 0)
        {
            Dictionary<Node, float> newDict = new Dictionary<Node, float>();

            foreach(Node n in growNodes.Keys)
            {
                newDict.Add(n, growNodes[n] - Time.deltaTime);
                if (newDict[n] <= 0)
                {
                    newDict.Remove(n);
                    growingNodes.Remove(n);
                    if(growAmountNodes[n] < 20)
                    {
                        growAmountNodes[n] += 5;
                        visualizeCrops(n);
                       
                    }
                    else
                    {
                        readyToReap.Add(n);
                    }
                }
            }
            growNodes.Clear();
            growNodes = newDict;
        }

        if(collectedCrops.Count >= farmNodes.Count)
        {
            collectedCrops.Clear();
            readyToReap.Clear();
        }
    }

    public void visualizeCrops(Node n)
    {
        Vector3 sameV3 = crops[n].transform.position;
        switch (growAmountNodes[n])
        {
            case 0:
                Instantiate(readyToHaulCrop, n.worldPosition, Quaternion.identity);
                crops[n].transform.position = new Vector3(sameV3.x, -5.25f, sameV3.z);
                break;
            case 5:
                crops[n].transform.position = new Vector3(sameV3.x, -0.25f, sameV3.z);
                crops[n].renderer.material = firstPhase;
                break;
            case 10:
                crops[n].transform.position = new Vector3(sameV3.x, 0f, sameV3.z);
                crops[n].renderer.material = secondPhase;
                break;
            case 15:
                crops[n].transform.position = new Vector3(sameV3.x, 0.25f, sameV3.z);
                crops[n].renderer.material = thirdPhase;
                break;
            case 20:
                crops[n].transform.position = new Vector3(sameV3.x, 0.5f, sameV3.z);
                crops[n].renderer.material = lastPhase;
                break;
        }
    }
    void OnDrawGizmos()
    {
        if(growingNodes.Count > 0)
        {
            foreach (Node n in growingNodes)
            {
                Gizmos.color = Color.yellow;
                Gizmos.DrawCube(n.worldPosition, Vector3.one);
            }
        }
        if (collectedCrops.Count > 0)
        {
            foreach (Node n in collectedCrops)
            {
                Gizmos.color = Color.black;
                Gizmos.DrawCube(n.worldPosition, Vector3.one);
            }
        }
    }

    public void addToGrowingNode(Node n)
    {
        growingNodes.Add(n);
        growNodes.Add(n, 10);
    }

    public bool allGrowing(Node myNode)
    {
        if(growingNodes.Count >= farmNodes.Count)
        {
            return true;
        }
        else if(growingNodes.Count + occupiedNodes.Count >= farmNodes.Count)
        {
            if(occupiedNodes.Contains(myNode))
            {
                return false;
            }
            return true;
        }
        return false;
    }

    public bool isReadyToReap(Node n)
    {
        if(!readyToReap.Contains(n))
        {
            return false;
        }
        return true;
    }
    public bool allReadyToReap()
    {
        if(readyToReap.Count >= farmNodes.Count)
        {
            return true;
        }
        return false;
    }

    public bool isFull(Node n)
    {
        if (occupiedNodes.Contains(n))
        {
            return false;
        }
        return true;
    }
    public bool isGrowing(Node n)
    {
        if (growingNodes.Contains(n))
        {
            return true;
        }
        return false;
    }
}

Try some Debug.Log statements to see what code on what objects still get called

Holy nested if statements, Batman! Debuggers nightmare. A good code practice is to keep nesting <= 2 ifs in any block. It might not sound like it, but keeping the IF checks but breaking them out into separate bits of logic and methods will REALLY help you debug. Really. Like really. :stuck_out_tongue:

1 Like