Not increasing/decreasing number?

I have made a simple script which should track how many entities are entering a building and how many entities are leaving the building. For some reason, the count of staffAtWork never changes, but the functions are being called.

public ServeFood serveFood;

    void Start()
    {
        serveFood = transform.parent.parent.GetComponent<ServeFood>();
    }

    void OnTriggerEnter(Collider other)
    {
        if(other.gameObject.GetComponent<CivilianInfo>().work == this.gameObject)
        {
            serveFood.staffAtWork = serveFood.staffAtWork + 1;
            Debug.Log(other.gameObject.GetComponent<CivilianInfo>().forename + " is working at " + transform.parent.parent.gameObject.name);
        }
    }

    void OnTriggerExit(Collider other)
    {
        if(other.gameObject.GetComponent<CivilianInfo>().work == this.gameObject)
        {
            serveFood.staffAtWork = serveFood.staffAtWork - 1;
            Debug.Log(other.gameObject.GetComponent<CivilianInfo>().forename + " is no longer working at " + transform.parent.parent.gameObject.name);
        }
    }
}

The script (ServeFood) it is looking to -

public class ServeFood : MonoBehaviour {

    public int surplusFoodValue;
    public bool isWorked; //Unworked service buildings do not distribute food
    public int staffAtWork; //Is someone currently working in the building?
    public float timer = 0;
    public float timerMax = 0.1f;

    EmployeeInfo employInfo;

    OccupationController occupationControl;

    void Start()
    {
        employInfo = GetComponent<EmployeeInfo>();
        occupationControl = GameObject.Find("OccupationControl").GetComponent<OccupationController>();
    }

    void Update()
    {
        if(employInfo.myWorkers.Count < employInfo.capacity)
        {
            if(isWorked == false && occupationControl.unemployedVillagers.Count > 0)
            {
                occupationControl.getJob(employInfo);
            }
        }
        else
        {
            occupationControl.removeFromList(gameObject);
            isWorked = true;
        }
    }
}

The first script does find the correct ServeFood component. It is Public, so I am able to check that within gameplay.

Any idea why staffAtWork isn’t changing from 0?

Thanks in advance.

by that do you mean the debug.log lines (### is working at ### , ### is no longer working at ###) are coming through in the console?

in passing

occupationControl =GameObject.Find("OccupationControl").GetComponent<OccupationController>();

assuming that there is only one occupation controller in your scene you can use “FindObjectOfType<>()” to do this in one call, avoids the potential typo/naming issues of the find.

occupationControl =(OccupationController)GameObject.FindObjectOfType<OccupationController>();
1 Like

Yes, it is being logged. Thanks for the info on linking up the controller. :slight_smile:

Edit: It was not being logged, I had a very similar Debug.Log which caused confusion.

Ha, found the problem. I originally had

if(other.gameObject.GetComponent<CivilianInfo>().work == this.gameObject)

When I should have used

if(other.gameObject.GetComponent<CivilianInfo>().work == transform.parent.parent.gameObject)

The object with the trigger is a grandchild of the object which is actually set as the Civilians place of work. Feed a bit stupid now :smile: