0
My objects are apparently being added to my list, but when I call the list in update() it does not perform as expected.
public class CenaCozinha : MonoBehaviour {
//Os Alimentos
public GameObject Alimento;
public GameObject Agua;
public GameObject Melancia;
public List<Alimentos> alimentosCozinha = new List<Alimentos>();
// Use this for initialization
void Start () {
InicializarAlimentos();
}
// Update is called once per frame
void Update () {
foreach (Alimentos al in alimentosCozinha) {
if (al.nomeAlimento == "agua") {
Agua.SetActive(true);
Debug.Log(al.nomeAlimento);
}
if (al.nomeAlimento == "melancia") { //ele não entra aqui
Melancia.SetActive(true);
Debug.Log(al.nomeAlimento);
}
}
}
public void InicializarAlimentos() { //aqui ele adiciona
Alimentos novoAlimento = Alimento.AddComponent<Alimentos>();
novoAlimento.id = "agua";
novoAlimento.nomeAlimento = "agua";
alimentosCozinha.Add(novoAlimento);
}
public void ComprarAgua() {
Alimentos novoAlimento = Alimento.AddComponent<Alimentos>();
novoAlimento.id = "agua";
novoAlimento.nomeAlimento = "agua";
alimentosCozinha.Add(novoAlimento);
}
public void ComprarMelancia() {
Alimentos novoAlimento = Alimento.AddComponent<Alimentos>();
novoAlimento.id = "melancia";
novoAlimento.nomeAlimento = "melancia";
alimentosCozinha.Add(novoAlimento);
}
} }
When, after Add, I put:
Debug.Log(alimentosCozinha.Count);
It shows that increased the count.
I’ve been waiting for a callback, I’ve had a long time with this problem... Thank you
I’m calling Buywatermelon(); by clicking on a button in Unity; In case the game already starts with Water added in the list (which is what happens in Initialize Food();) But, by clicking on Buywatermelon, it apparently does not add... even the 'Debug.Log(foodCozinha.Count);' showing that increased the count. Ps: When I call the Buy watermelon() function straight in Update, it adds normal and enters if.
– Emerson Barbosa
Got it, I was putting a different object in Unity. The code is correct, the problem was in Unity. Thanks ;D
– Emerson Barbosa
Good.. So, since I couldn’t see where Buywatermelon was called, I didn’t have much to help haha.. But I’m glad it worked haha.
– Marcos Defina