Counting Number of random objects

Hello everyone, I am still new to Unity.

I wish to know whether there is a way to count random objects on Unity and display them in the GUI display.
For example:
I have 3 coloured spheres that randomly spawn in 3 different locations (In an empty game object) and they are then destroyed. It is like a repeating process.

However, I wish to count all the different spheres that appears and display them on my the canvas (panel).

Thanks for helping.

Take a look at the following two classes

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

public static class ExtensionMethods
{
    public static T FindObjectOfType <T> (this UnityEngine.Object unityObject) where T : UnityEngine.Object
    {
        return UnityEngine.Object.FindObjectOfType(typeof(T)) as T;
    }

    public static T[] FindObjectsOfType <T> (this UnityEngine.Object unityObject) where T : UnityEngine.Object
    {
        return UnityEngine.Object.FindObjectsOfType(typeof(T)) as T[];
    }
}

and

// using FindObjectsOfType() from above, find all MeshFilter components
// and determine if they are spheres
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Test : MonoBehaviour
{
    public List<MeshFilter> meshFilters;

    private int numSpheres = 0;

    void Start()
    {
        numSpheres = 0;
        meshFilters = new List<MeshFilter>(FindObjectsOfType<MeshFilter>());
        if (meshFilters.Count > 0)
        {
            for (int i = 0; i < meshFilters.Count; i++)
            {
                if (meshFilters*.mesh.name == "Sphere Instance")*

{
numSpheres++;
}
}
}
Debug.Log("Number of Spheres Found = " + numSpheres);
}
}
The first class ExtensionMethods.cs is a Unity extension class that allow you to search for all kind of objects using the methods FindObjectOfType() and FindObjectsOfType(), the latter returns an array.
The second class is a test class (Test.cs) that uses FindObjectsOfType() in the Start() function to find all MeshFilters() (all Spheres will require one) it then checks to see if the Mesh name is equal to Sphere Instance (this is what a Sphere’s Mesh name will be).
It does this in a loop and increments the variable numSpheres for each Sphere Instance found.
It then prints the result to the console using Debug.Log().
If you just drop these two classes in your game and then add the *Test Class to any GameObject you will then be able to see the number of spheres.
You can take this a step farther by replaceing the loop and the Debug.Log() statement with the following
for (int i = (meshFilters.Count - 1); i >= 0; i–)
{
if (meshFilters*.mesh.name != “Sphere Instance”)*
{
meshFilters.RemoveAt(i);
}
}

Debug.Log("Number of Spheres Found = " + meshFilters.Count);
The loop will go through and remove all meshes that are not Sphere’s. You will now have a list of only Sphere meshes that you can reference and there is no need for the extra variable numSpheres.