How to get all objects instantiated from a single prefab?

I want to use GameObject.FindGameObjects that have been instantiated in my scene from a single prefab.

I am using a button to generate cubes, and I want to be able to always have all those cubes do something… Like if I have cubes and spheres and cylinders in the scene I want to be able to make all of them separate colors so that I can differentiate between them. How do I grab all of the game objects from only one prefab in a script?

There’s more than one way, here’s two:

// add a tag to the cube prefab you are spawning in the scene
GameObject[] cubes = GameObject.FindGameObjectsWithTag("tagToSearchFor");

// add the cube to a list when you instantiate it
List<GameObject> cubes = new List<GameObject>(); 
GameObject cube = Instantiate(cubePrefab, position, rotation);
cubes.Add(cube);