Search Unity

Why can I not do a foreach in my List of gameobjects?

Discussion in 'Scripting' started by OffThHeezay91, Oct 31, 2019.

  1. OffThHeezay91

    OffThHeezay91

    Joined:
    Feb 23, 2013
    Posts:
    45
    I have a list of gameobjects and I want to cycle through the list and disable each object.

    public List<GameObject> panel;
    foreach (GameObject i in panel) { i.SetActive(false); }


    I am getting the error that I cannot convert char to gameobject. How do I refer to the gameobject?
    thank you
     
  2. Deleted User

    Deleted User

    Guest

    Your script works for me:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Test : MonoBehaviour
    6. {
    7.     public List<GameObject> panel;
    8.  
    9.     private void Start()
    10.     {
    11.         foreach(GameObject i in panel) { i.SetActive(false); }
    12.     }
    13. }
     
  3. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    What does the rest of your script look like? Are you sure the error is on that line?
     
  4. OffThHeezay91

    OffThHeezay91

    Joined:
    Feb 23, 2013
    Posts:
    45
    I see my mistake now, I used "panel" 2 times which caused the error :confused:

    public List<GameObject> panel;
    public void ChangePanel(string panel)
    {
    foreach (GameObject i in panel) {
    i.SetActive(false);
    }
    }


    Thanks for testing the code all
     
    Joe-Censored likes this.