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.
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.
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).
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.