Foreach a list of objects

Hi! i have this situation

public GameObject ObjetoAOcultar1, ObjetoAOcultar2, ObjetoAOcultar3, ObjetoAOcultar4, ObjetoAOcultar5;


ObjetoAOcultar1.GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f, alphaLevel);
            ObjetoAOcultar2.GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f, alphaLevel);
            ObjetoAOcultar3.GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f, alphaLevel);
            ObjetoAOcultar4.GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f, alphaLevel);
            ObjetoAOcultar5.GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f, alphaLevel);

how can i transform this collection into a foreach loop?

This will turn it into a collection using a public array that you drag and drop into

public GameObject[] ObjetoAOcultar;

foreach(GameObject objet in ObjetoAOcultar)
{
   objet.GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f, alphaLevel);
}
1 Like
public List<GameObject> List;

foreach (var x in List)
{
    x.GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f, alphaLevel);
}
2 Likes

here you go man.

using UnityEngine;
using System.Collections.Generic;

public class forEachLoop : MonoBehaviour {


    public GameObject ObjetoAOcultar1, ObjetoAOcultar2, ObjetoAOcultar3, ObjetoAOcultar4, ObjetoAOcultar5;

    public List<GameObject> ObjectList;
    public int alphaLevel = 100;


    // Use this for initialization
    void Start () {
        ObjectList = new List<GameObject>();
        ObjectList.Add(ObjetoAOcultar1);
        ObjectList.Add(ObjetoAOcultar2);
        ObjectList.Add(ObjetoAOcultar3);
        ObjectList.Add(ObjetoAOcultar4);
        ObjectList.Add(ObjetoAOcultar5);

        //for loop
        for (int i = 0; i < ObjectList.Count; i++)
        {
            ObjectList[i].GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f, alphaLevel);
        }

        //foreach loop
        foreach (GameObject tempObject in ObjectList)
        {
            tempObject.GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f, alphaLevel);
        }
    }

}
3 Likes

hey thanks for reply!
this is the entirely script, is correct?

using UnityEngine;
using System.Collections;
public class HideObjects : MonoBehaviour
{

    bool isInside;
    float alphaLevel;
    public float alphaFactor;
    public GameObject[] ObjectoAOcultar;
    void Start()
    {
    }

    void OnTriggerEnter2D(Collider2D collider)
    {
        isInside = true;
    }

    void OnTriggerExit2D(Collider2D collider)
    {
        isInside = false;
    }

    void Update()
    {
        if (isInside == true)
        {
            if (alphaLevel < 0.25f)
                alphaLevel = 0.0f;
            else
                alphaLevel -= alphaFactor * Time.deltaTime;
            foreach (GameObject objeto in ObjetoAOcultar)
            {
                objeto.GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f, alphaLevel);
            }
        }
        if (isInside == false)
        {
            if (alphaLevel > 0.75f)
                alphaLevel = 1.0f;
            else
                alphaLevel += alphaFactor * Time.deltaTime;

            foreach (GameObject objeto in ObjetoAOcultar)
            {
                objeto.GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f, alphaLevel);
            }

        }
    }
}

visual studio show me that ObjetoAOcultar does not exist in the context

You named the collection ObjectoAOcultar but in the foreach loop you are using ObjetoAOcultar. They need to be named the same.

1 Like

Oohh, spanglish power gahahahaha, thanks you

Ok Im well out of date here but I just came across this. List appears to have its own built in ForEach method. If someone wants to verify this, all gravy.

https://msdn.microsoft.com/en-us/library/bwabdf9z(v=vs.110).aspx

2 Likes

Correct - the builtin foreach works as well.