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;
}
}
}
}
}