[SOLVED]Optimisation Of OnTriggerstay For Traffic Lights

Hi guys,

Can I use another method instead of OnTriggerStay?
My car needs to know all time if the color is green, yellow or red.
Here is my old code:

    /// Check if the car is near trafficlight
    /// </summary>
    /// <param name="other"></param>
    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("Car"))
        {
            if ((_yellow.enabled == true) || (_red.enabled == true))
                other.gameObject.GetComponent<CarEngine>().IsStop = true;
            else
                other.gameObject.GetComponent<CarEngine>().IsStop = false;
        }
    }

    /// <summary>
    /// Check if the car is near trafficlight
    /// </summary>
    /// <param name="other"></param>
    private void OnTriggerStay(Collider other)
    {

        if (other.gameObject.CompareTag("Car"))
        {

            if (_green.enabled == true)
                other.gameObject.GetComponent<CarEngine>().IsStop = false;

            else if ((_yellow.enabled == true) || (_red.enabled == true))
                other.gameObject.GetComponent<CarEngine>().IsStop = true;

        }
    }

    /// <summary>
    /// Check if the car is not near trafficlight
    /// </summary>
    /// <param name="other"></param>
    private void OnTriggerExit(Collider other)
    {
        if (other.gameObject.CompareTag("Car"))
        {
            other.gameObject.GetComponent<CarEngine>().IsStop = false;
        }

    }

This is the single option that I have in mind:

    /// <summary>
    /// Update is called every frame, if the MonoBehaviour is enabled.
    /// </summary>
    private void Update()
    {
        if (_green == true)
            foreach (var car in _listWithCars)
                car.GetComponent<CarEngine>().IsStop = false;
    }

    /// <summary>
    /// Check if the car is near trafficlight
    /// </summary>
    /// <param name="other"></param>
    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("Car"))
        {
            _listWithCars.Add(gameObject);
            if ((_yellow.enabled == true) || (_red.enabled == true))
                other.gameObject.GetComponent<CarEngine>().IsStop = true;
            else
                other.gameObject.GetComponent<CarEngine>().IsStop = false;
        }
    }

    /// <summary>
    /// Check if the car is not near trafficlight
    /// </summary>
    /// <param name="other"></param>
    private void OnTriggerExit(Collider other)
    {
        if (other.gameObject.CompareTag("Car"))
        {
            if (_listWithCars.Contains(other.gameObject))
                _listWithCars.Remove(other.gameObject);
            other.gameObject.GetComponent<CarEngine>().IsStop = false;
        }

    }

You could add the tradficlight as a variable and remove it on enter and exit and then check in update for example?

Don’t use getcomponent each frame, it is expensive. Either find the car in ontriggerenter and add it to a list of cars then check through that list or add a reference to the lights to the car when they enter and check in the car class.

Well, i need to know what gameobject is on trigger, so i have to use getComponent.

You should add the car to your light and if it gets green run your car script, dont the other way around

I have over 400 cars and 30 traffic lights. I don’t think this is a good idea.

Well if the car hits the traffic light collider, add it to the traffic light and do your functions there until it exits the collider, then release, stop executing your behaviour

I think you misunderstood, I’m saying don’t use it every frame. It isn’t how I would do it myself as the cars will need logic to stop behind other cars so I would probably just raycast. However, you could add/remove the cars from a list when they enter/exit.

    private List<CarEngine> _cars = new List<CarEngine>();

    private void Update()
    {
        var shouldStop = !_green.enabled;

        foreach (var car in _cars)
        {
            car.IsStop = shouldStop;
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        var car = other.GetComponent<CarEngine>();

        if(car != null && !_cars.Contains(car))
        {
            _cars.Add(car);
        }
    }

    private void OnTriggerExit(Collider other)
    {
        var car = other.GetComponent<CarEngine>();

        if (car != null && _cars.Contains(car))
        {
            _cars.Remove(car);
        }