Triple nested for loops?

Is the following code ok to use?

//The object to be spawned
public GameObject[] crystal;

//The list which holds all the crystals
public List<GameObject> crystals;

public GameObject crystalParent;

int pooledAmount = 20;

void Start()
{
	crystals = new List<GameObject>();

	for (int i = 0; i < pooledAmount; i++)
	{
		for (int j = 0; j < crystal.Length; j++)
		{
			crystals.Add((GameObject)Instantiate(crystal[j]));

			for (int k = 0; k < crystals.Count; k++)
			{
				crystals[k].SetActive(false);
				crystals[k].transform.parent = crystalParent.transform;
			}
		}
	}
}

It is a triple nested for loop, I need it to Instantiate 4 different objects 20 times, then iterate through each one, disable it and parent it to a specified empty object. It works ok but I was just wondering if triple nested for loops are something that are used?

There’s no problem in general with nested loops. It all comes down to what you do inside that loop and how many iterations you have.

In your specific case here however you don’t need the innermost loop at all. For each object you create you iterate through all already created objects and disable and parent them again and again.

You could simply do:

 crystals = new List<GameObject>();
 Transform parent = crystalParent.transform;
 for (int i = 0; i < pooledAmount; i++)
 {
     for (int j = 0; j < crystal.Length; j++)
     {
         GameObject inst = (GameObject)Instantiate( crystal[j] );
         inst.SetActive(false);
         inst.transform.parent = parent;
         crystals.Add( inst );
     }
 }

Keep in mind that Instantiate can be quite demanding. So if your prefabs are quite complex your loop might freeze your game for some time (depending on your hardware that could be just a few milliseconds or even several seconds). However i guess you run that loop just once at the game start, so it shouldn’t be a problem ^^.