Arrays in foreach loops

I was wondering if it was possible to use arrays in a foreach loop? I have searched up a bit on it. But the only thing close to what I could find is an example from this webpage which uses an abstract array instead of a singular dimensional array, like the ones in the “myClass” class.

public class myClass {
    public Gameobject[] pencils
    public Gameobject[] rules;
    public Gameobject[] erasers;
}

public class StationaryChooser : MonoBehaviour {

    public string stationaryName;

    public void OnGUI() {
        foreach(Gameobject[] g in myClass) {
            if (g.lenght > 0) {
                for(int i = 0; i < g.lenght; i++) {
                    if (GUI.Button(new Rect(10 + (110 * i), 10, 100, 60), g[i].name)) {
                        stationaryName = g[i].name; 
                    }
                }
            }
        }
    }
}

This example shown above is the basis of what I am trying to achieve.

Any help would be thankful :smile:

Basically you want to iterate over an object or static class, it is a bit tricky to do.

You could create a method to get all objects, I used List<>, but you can change to Array.
I have not tested the code, but you might get the idea.

public class myClass 
{
    public Gameobject[] pencils;
    public Gameobject[] rules;
    public Gameobject[] erasers;

    public List<Gameobject[]> GetObjects()
    {
        var objects = new List<Gameobject[]>();
        objects.Add(pencils);
        objects.Add(rules);
        objects.Add(erasers);
        return objects;
    }
}


public class StationaryChooser : MonoBehaviour 
{
    public string stationaryName; 

    public void OnGUI() {
        foreach(var g in myClass.GetObjects()) 
        {
            foreach(var obj in g)
            {
                if (GUI.Button(new Rect(10 + (110 * i), 10, 100, 60), obj .name)) 
                {
                    stationaryName = obj .name; 
                }
            }            
        }
    }
}

@maikonfarias Sorry for the late reply, But I cant seem to understand how this works? :face_with_spiral_eyes:.
I have searched the internet for a while now, and have not come up with any results concerning this.
Your example looks functional in a workings of it, but I don’t know how to implement it into my current script unfortunately, it also throws up quite a few errors.

@djfunkey, in the example you can gave, you think that you can access the public fields in your ‘myClass’ class by their type, this is by your usage of the foreach loop:

...
foreach(Gameobject[] g in myClass) {

            if (g.lenght > 0) {

                for(int i = 0; i < g.lenght; i++) {

                    if (GUI.Button(new Rect(10 + (110 * i), 10, 100, 60), g[i].name)) {

                        stationaryName = g[i].name; 

                    }

                }

            }

        }
...

This is incorrect with the exception of using reflection to go over the properties. The example maikonfarias gave included a convenience method to add your public properties (pencils, rules, erasers) to a List of GameObject[ ] through a new method in the ‘myClass’ class called GetObjects. If that method is called in the form maikonfarias wrote it, it would return a List with three elements/items/thingers in it, index 0 would be the pencils GameObject array, index 1 would be the rules GameObject array and finally the last, index 2 would be erasers GameObject Array.

@Landern: Good explanation.

The method GetObjects() is like a shortcut to your objects.

For my code to work I need more context what it is about, for example, the class myClass and its methods/members should be static, or you would need a object to access myClass.

public static class myClass 
{
    public static Gameobject[] pencils;
    public static Gameobject[] rules;
    public static Gameobject[] erasers;

    public static List<Gameobject[]> GetObjects()
    {
        var objects = new List<Gameobject[]>();
        objects.Add(myClass.pencils);
        objects.Add(myClass.rules);
        objects.Add(myClass.erasers);
        return objects;
    }
}

or

public class StationaryChooser : MonoBehaviour 
{
    public string stationaryName; 
    
    // get myClass object access if it is in the same object
    var  objectMyClass = this.gameObject.GetComponent<myClass>(); 

    public void OnGUI() {
        foreach(var g in objectMyClass.GetObjects()) 
        {
            foreach(var obj in g)
            {
                if (GUI.Button(new Rect(10 + (110 * i), 10, 100, 60), obj.name)) 
                {
                    stationaryName = obj.name; 
                }
            }            
        }
    }
}

@maikonfarias Thank you a lot!! You helped so much. I finally figured out my problem…
I only had:

using UnityEngine;
using System;

Which was making every List collection through up an error. A silly mistake on my part sorry!

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;

public class myClass {
    public Gameobject[] pencils;
    public Gameobject[] rules;
    public Gameobject[] erasers;

    public List<Gameobject[]> GetObjects()
    {
        var objects = new List<Gameobject[]>();
        objects.Add(pencils);
        objects.Add(rules);
        objects.Add(erasers);
        return objects;
    }
} 

public class StationaryChooser : MonoBehaviour { 

    private myClass mClass;

    public string stationaryName;

    void Awake() {
	mClass = GetComponent<myClass>();
    } 

    public void OnGUI() {
        foreach(var g in myClass.GetObjects) {
            if (g.lenght > 0) {
                for(int i = 0; i < g.lenght; i++) {
                    if (GUI.Button(new Rect(10 + (110 * i), 10, 100, 60), g[i].name)) {
                        stationaryName = g[i].name; 
                    }
                }
            }
        }
    }
}

@maikonfarias I have run into a problem with my Script and I am trying to convert it from List<> to Array, But I am unsure how to convert it. You also mentioned I should do this in your original post.

I have tried using ArraySegment, Array, and ArrayList, In different places within the script, but they are fairly new to me and I don’t think I am implementing them right. Could you please provide some assistance. :wink: