Add Object to a List with C#

Asked

Viewed 289 times

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

2 answers

2


You are not adding Watermelon to your list, so the second Update If will never be true. There are a few more logic errors in your script, which I’m sorry I couldn’t help but be without Unity, and in my head I’m prone to mistakes. So not to talk shit, I’ll just point out what I saw of the log problem.

The Initialize Food function only adds water, and I haven’t found anywhere else the call for the purchase functions... so nothing else is added except water in your list... Try replacing your startup with something like:

public void InicializarAlimentos() {
    ComprarAgua();
    ComprarMelancia();
}

Put a Debug.log() in the update, and call these functions to see if they work... If it doesn’t work, there are more errors there related to this list object creation..

Good luck aew.

  • 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.

  • Got it, I was putting a different object in Unity. The code is correct, the problem was in Unity. Thanks ;D

  • 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.

0

Try this!

public class CenaCozinha : MonoBehaviour {

   //Os Alimentos
   public GameObject Alimento;
   public GameObject Agua;
   public GameObject Melancia;
   public List<Alimentos> alimentosCozinha;    

  // Use this for initialization
  void Start () {
     alimentosCozinha = new List<Alimentos>();
  }

  // Update is called once per frame
  void Update () {

     foreach (Alimentos al in alimentosCozinha) {
        if (al.nomeAlimento == "agua") {
           Agua.SetActive(true);
           Debug.Log(al.nomeAlimento);
        }
        else if (al.nomeAlimento == "melancia") {
           Melancia.SetActive(true);
           Debug.Log(al.nomeAlimento);
        }
     }
  }

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

Or if you prefer to use InicializarAlimentos()...

public void InicializarAlimentos() {
   alimentosCozinha = new List<Alimentos>();
}
  • Unfortunately the 'error' continues...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.