Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Foreach a list of objects

Discussion in 'Scripting' started by Gian519, Jan 26, 2017.

  1. Gian519

    Gian519

    Joined:
    Mar 25, 2016
    Posts:
    9
    Hi! i have this situation


    Code (CSharp):
    1. public GameObject ObjetoAOcultar1, ObjetoAOcultar2, ObjetoAOcultar3, ObjetoAOcultar4, ObjetoAOcultar5;
    2.  
    3.  
    4. ObjetoAOcultar1.GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f, alphaLevel);
    5.             ObjetoAOcultar2.GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f, alphaLevel);
    6.             ObjetoAOcultar3.GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f, alphaLevel);
    7.             ObjetoAOcultar4.GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f, alphaLevel);
    8.             ObjetoAOcultar5.GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f, alphaLevel);
    how can i transform this collection into a foreach loop?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    This will turn it into a collection using a public array that you drag and drop into

    Code (CSharp):
    1.  
    2. public GameObject[] ObjetoAOcultar;
    3.  
    4. foreach(GameObject objet in ObjetoAOcultar)
    5. {
    6.    objet.GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f, alphaLevel);
    7. }
    8.  
     
    ow3n likes this.
  3. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,384

    Code (csharp):
    1.  
    2. public List<GameObject> List;
    3.  
    4. foreach (var x in List)
    5. {
    6.     x.GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f, alphaLevel);
    7. }
     
    Gian519 likes this.
  4. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    here you go man.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3.  
    4. public class forEachLoop : MonoBehaviour {
    5.  
    6.  
    7.     public GameObject ObjetoAOcultar1, ObjetoAOcultar2, ObjetoAOcultar3, ObjetoAOcultar4, ObjetoAOcultar5;
    8.  
    9.     public List<GameObject> ObjectList;
    10.     public int alphaLevel = 100;
    11.  
    12.  
    13.     // Use this for initialization
    14.     void Start () {
    15.         ObjectList = new List<GameObject>();
    16.         ObjectList.Add(ObjetoAOcultar1);
    17.         ObjectList.Add(ObjetoAOcultar2);
    18.         ObjectList.Add(ObjetoAOcultar3);
    19.         ObjectList.Add(ObjetoAOcultar4);
    20.         ObjectList.Add(ObjetoAOcultar5);
    21.  
    22.         //for loop
    23.         for (int i = 0; i < ObjectList.Count; i++)
    24.         {
    25.             ObjectList[i].GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f, alphaLevel);
    26.         }
    27.  
    28.         //foreach loop
    29.         foreach (GameObject tempObject in ObjectList)
    30.         {
    31.             tempObject.GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f, alphaLevel);
    32.         }
    33.     }
    34.    
    35. }
     
  5. Gian519

    Gian519

    Joined:
    Mar 25, 2016
    Posts:
    9
    hey thanks for reply!
    this is the entirely script, is correct?

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. public class HideObjects : MonoBehaviour
    5. {
    6.  
    7.     bool isInside;
    8.     float alphaLevel;
    9.     public float alphaFactor;
    10.     public GameObject[] ObjectoAOcultar;
    11.     void Start()
    12.     {
    13.     }
    14.  
    15.     void OnTriggerEnter2D(Collider2D collider)
    16.     {
    17.         isInside = true;
    18.     }
    19.  
    20.     void OnTriggerExit2D(Collider2D collider)
    21.     {
    22.         isInside = false;
    23.     }
    24.  
    25.     void Update()
    26.     {
    27.         if (isInside == true)
    28.         {
    29.             if (alphaLevel < 0.25f)
    30.                 alphaLevel = 0.0f;
    31.             else
    32.                 alphaLevel -= alphaFactor * Time.deltaTime;
    33.             foreach (GameObject objeto in ObjetoAOcultar)
    34.             {
    35.                 objeto.GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f, alphaLevel);
    36.             }
    37.         }
    38.         if (isInside == false)
    39.         {
    40.             if (alphaLevel > 0.75f)
    41.                 alphaLevel = 1.0f;
    42.             else
    43.                 alphaLevel += alphaFactor * Time.deltaTime;
    44.  
    45.             foreach (GameObject objeto in ObjetoAOcultar)
    46.             {
    47.                 objeto.GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f, alphaLevel);
    48.             }
    49.  
    50.         }
    51.     }
    52. }
    visual studio show me that ObjetoAOcultar does not exist in the context
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    You named the collection ObjectoAOcultar but in the foreach loop you are using ObjetoAOcultar. They need to be named the same.
     
    Gian519 likes this.
  7. Gian519

    Gian519

    Joined:
    Mar 25, 2016
    Posts:
    9
    Oohh, spanglish power gahahahaha, thanks you
     
  8. meat5000

    meat5000

    Joined:
    Mar 5, 2013
    Posts:
    118
    sj631 and TooManySugar like this.
  9. PurpledArtFrog

    PurpledArtFrog

    Joined:
    Aug 6, 2018
    Posts:
    4
    Correct - the builtin foreach works as well.