How to add every instantiated prefab to an array?

Hi! I’m trying to do something that I don’t really know how to do it, I hope you could help me.

So I have instantiate “ValueDaño”, which is a Text. I want to add every new instance of “ValueDaño” to the array “Textos”, so I can control their position with the foreach loop until each one is destroyed.

How can I do that??

public void NumDamages()
    {

        if (ValorVida != enemigo.GetComponent<enemigo>().Vida)
        {

            ValueDaño = Instantiate(textoD, Camera.main.WorldToScreenPoint(enemigo.position + offset2), transform.rotation);
           Destroy(ValueDaño.gameObject, 3);
            ValorVida = enemigo.GetComponent<enemigo>().Vida;
  
        }

   
        Text [] Textos;

        foreach (Text item in Textos)
        {
            ValueDaño.transform.position += Vector3.up * ValorSubida * Time.deltaTime;
        }


  }

If you want dynamically add items to array, it is easier to use list. Array is fixed-size storage, and list is variable size storage.

private List<Text> Textos = new List<Text>();

public void NumDamages()
    {
        if (ValorVida != enemigo.GetComponent<enemigo>().Vida)
        {
            ValueDaño = Instantiate(textoD, Camera.main.WorldToScreenPoint(enemigo.position + offset2), transform.rotation);
       
           Textos.Add(ValueDaño);

           Destroy(ValueDaño.gameObject, 3);
            ValorVida = enemigo.GetComponent<enemigo>().Vida;
        }
        foreach (Text item in Textos)
        {
            ValueDaño.transform.position += Vector3.up * ValorSubida * Time.deltaTime;
        }
  }
1 Like

Do not forget to remove them when appopriate too

Wow!! Thanks!! It worked fine!!!