For loops and arrays

Hi everyone,

I am attempting to do a for loop that runs through my class which contains an array and then switch off all the game objects in the array.

My class contains multiple groups. For example this is for an evolution system I want to implement. Where each group is an evolutions (evo1, evo2, evo3 etc). I want the game objects to be disabled unless the players is the correct evolution.

This is what my class looks like:

//Class to hold evo group objects
    [System.Serializable]
    public class MyEvolution
    {
        public GameObject[] _gameObjects;
    }
   
    public MyEvolution[] _myEvolutions;

I then tried to make a function that would go through each array and switch off all the game objects which is not correct.

void DisableAll()
    {
        for(int i = 0; i < _myEvolutions._gameObjects.Length; i++)
        {
            _myEvolutions._gameObjects[i];
        }
    }

I also need to create another function that will go through just one array e.g. evo1 and enable each gameobject in that array.

I think im close but also not done this before so quite confused so any help would be greatly appreciated.

Thank you

public class MyEvolution
    {
        public GameObject[] _gameObjects;

        public void DisableAllGameObjects()
        {
            foreach(GameObject g in _gameObjects)
            {
                g.SetActive(false);
            }
        }
    }
   
    public MyEvolution[] _myEvolutions;


    public void DisableAllEvolutions()
    {
        foreach(MyEvolution e in _myEvolutions)
        {
            e.DisableAllGameObjects();
        }
    }

This is how i would do it. As you can see MyEvolution has a method itself to disable all its game objects, all you need to do then is loop round all the evolutions and call that method on each evolution.

1 Like

Thats awesome thank you.

So if I wanted to just enable all the game objects in evo3 would I do something like:

public void EnableEvoObj (_myEvolutions evo)
{
     foreach(GameObject g in evo)
          {
                g.SetActive(true);
          }
}

Or is that totally wrong?

    public class MyEvolution
    {
        public GameObject[] _gameObjects;
       
        public void DisableOrEnableAllGameObjects(bool _enable)
        {
            foreach(GameObject g in _gameObjects)
            {
                g.SetActive(_enable);
            }
        }

        
    }
   
    public MyEvolution[] _myEvolutions;
   
   
    public void DisableOrEnableAllEvolutions(bool _enable)
    {
        foreach(MyEvolution e in _myEvolutions)
        {
            e.DisableAllGameObjects(_enable);
        }
    }

   // this would be ev3 for example, and enable all the game objects for it
   _myEvolutions[2].DisableOrEnableAllGameObjects(true);

Ok so I just changed it a bit. The method can disable or enable the game objects depending on what you pass it (true of false).

1 Like

Brilliant thanks again. Couple of questions:

First the example:

// this would be ev3 for example, and enable all the game objects for it
   _myEvolutions[2].DisableOrEnableAllGameObjects(true);

Where would this go in the code? (Sorry i dont want this to feel like im asking you to do the work for me but I’m trying to learn)

Also what is the benefit of doing the method within the class as opposed to doing it inside a separate function?

Thanks again

That code needs to go in a function. It wontt compile like that at the moment - I was just illustrating how you could target just one evolution. Im guessing you will need to put it in an Update call in a game object somewhere. Im not sure how your game works so I cant really say. The code I have shown you is just the framework to enable or disable a group of game objects that belong to one of your evolution objects.

1 Like

Great. I will have a play around with this. Thanks for your help

Also can I just check that on line 23. (e.DisableAllGameObjects(_enable):wink: is this correct or should it be (e.DisableOrEnableAllGameObjects(_enable) ?

Should be DisableOrEnableAllGameObjects. My bad! Sorry!

Cheers Korno.