Foreach loop doesn't run

Hi

I’m trying to make a street intersection where I have cars going left and another group going up, in the middle has a headlight that controls when one side of the headlight is allowed and the other not.
In cars there is an enum that shows which direction they will go (horizontal or vertical) and in my code I have a function calling the cars method to be added to a list, then when all cars are stored I call another method that check how many cars each side has, I do this through a foreach loop, however my foreach loop is not called, I have tried to use for and yet nothing I do enters the loop the “VerificarQuantidadeDeCarros” function is called normally.

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

public class IAFarol : MonoBehaviour
{
    public bool ligarPrimeiroFarol;
    public bool ligarSegundoFarol;
    private bool ligado;

    [SerializeField] private GameObject[] _farois = new GameObject[2];
    [SerializeField] private List<GameObject> _carros = new List<GameObject>();

    // Start is called before the first frame update
    void Start()
    {
        ligarPrimeiroFarol = false;
        ligarSegundoFarol = false; 

        for(byte i = 0; i < 2; i++)
        {
            _farois *= GameObject.Find("Farol" + (i + 1).ToString());*

}
}

// Update is called once per frame
void Update()
{
if(ligarSegundoFarol && !ligado)
{
ligarPrimeiroFarol = false;
ligado = true;

Farol farol = _farois[0].GetComponent();
Farol farol2 = _farois[1].GetComponent();

farol.sinalDoFarol = Sinal.VERMELHO;
farol2.sinalDoFarol = Sinal.VERDE;
}
else if(ligarPrimeiroFarol)
{
ligarSegundoFarol = false;
ligado = false;

Farol farol = _farois[0].GetComponent();
Farol farol2 = _farois[1].GetComponent();

farol.sinalDoFarol = Sinal.VERDE;
farol2.sinalDoFarol = Sinal.VERMELHO;

}
}

public void ReceberCarro(GameObject carro)
{
_carros.Add(carro);
}

public void VerificarQuantidadeDeCarros()
{
byte _carrosNaHorizontal = 0;
byte _carrosNaVertical = 0;

foreach(GameObject carro in _carros)
{
PosFarol _posFarol = carro.GetComponent().posFarol;
if(_posFarol == PosFarol.HORIZONTAL)
{
_carrosNaHorizontal++;
}
else if(_posFarol == PosFarol.VERTICAL)
{
_carrosNaVertical++;
}
}

Debug.Log(“Verificou a quantidade de carros e mandou ligar o farol”);
Debug.Log("Carros na Horizontal: " + _carrosNaHorizontal);
Debug.Log("Carros na Vertical: " + _carrosNaVertical);
_LigarFarol(_carrosNaVertical, _carrosNaHorizontal);
}

private void _LigarFarol(byte qtdCarrosVertical, byte qtdCarrosHorizontal)
{
if(qtdCarrosHorizontal > qtdCarrosVertical)
{
ligarPrimeiroFarol = true;
float ftime = (float)qtdCarrosHorizontal + 5.0f / qtdCarrosVertical;
StartCoroutine(IniciarCarros(ftime));
}
else
{
ligarSegundoFarol = true;
float ftime = (float)qtdCarrosVertical + 5.0f / qtdCarrosHorizontal;
StartCoroutine(IniciarCarros(ftime));
}
}

IEnumerator IniciarCarros(float ftime)
{
if(ligarPrimeiroFarol)
{
foreach(GameObject car in _carros)
{
Carros script = car.GetComponent();

if (script.posFarol == PosFarol.HORIZONTAL)
{
script.podeIr = true;
}
}
}
else if(ligarSegundoFarol)
{
foreach (GameObject car in _carros)
{
Carros script = car.GetComponent();

if (script.posFarol == PosFarol.VERTICAL)
{
script.podeIr = true;
}
}
}
yield return new WaitForSeconds(ftime);

if(ligarPrimeiroFarol)
{
ligarSegundoFarol = true;

foreach (GameObject car in _carros)
{
Carros script = car.GetComponent();

if (script.posFarol == PosFarol.VERTICAL)
{
script.podeIr = true;
}
}
}
else if (ligarSegundoFarol)
{
ligarPrimeiroFarol = true;

foreach (GameObject car in _carros)
{
Carros script = car.GetComponent();

if (script.posFarol == PosFarol.HORIZONTAL)
{
script.podeIr = true;
}
}
}
}
}

I don’t think it’s a good idea to mix Coroutines and Foreachs. foreach assumes a stable list, and if you’re removing cars from the list as they cross the light, it’d break the foreach. That being said, I think it shouldn’t prevent the loop from starting, (though maybe it does).

There’s nothing in this code that seems to call ReceberCarro (maybe there’s somewhere else). Judging by this script alone, it seems the car list is always empty. So, the two questions are “is it starting the coroutine?” and “are there any cars in the list?”.

I’d suggest replacing the foreach with a for, and adding a log with the cars count at the very beginning of the coroutine to try and identify what the issue is.

If the method VerificarQuantidadeDeCarros is called and the foreach is not run once, there is one possibility left: the list _carros is empty. Why depends of your code, did you correctly called ReceberCarro before verifying the number of cars, are you sure that your coroutines don’t mess with the list, filling it too late or emptying to early ?

You can remove the else if condition for vertical vehicles since there are only 2 relevant axises to your situation, this way you will see if you have an abnormal number of vertical cars.