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.
Answer by TBruce · Jun 27, 2016 at 06:22 PM
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[i].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[i].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.
After following this tutorial by xOcto$$anonymous$$anx: https://www.youtube.com/watch?v=iLTP4Eb$$anonymous$$1N4
He made random spawn of coins which is then destroyed after a few seconds. Based on this method, I wanted to add a counting meter for every sphere. $$anonymous$$y case, i modified the coins to a sphere. So on my game screen I want to display a text that shows how many spheres were generated.
Thanks for replying. :)
I am sorry if I misunderstood you but the question you asked was
I wish to count all the different spheres that appears and display them on my the canvas
You did not mention how you wanted to display the information.
I did however provide you with what you need to accomplish your goal. From the code above as seen below
meshFilters = new List<$$anonymous$$eshFilter>(FindObjectsOfType<$$anonymous$$eshFilter>());
for (int i = (meshFilters.Count - 1); i >= 0; i--)
{
if (meshFilters[i].mesh.name != "Sphere Instance")
{
meshFilters.RemoveAt(i);
}
}
you will end up with a list of $$anonymous$$eshFilter's (List).
From here you have the number of spheres - meshFilters.Count
You can access the individual GameObjects of these spheres
for (int i = 0; i < meshFilters.Count; i++)
{
GameObject go = meshFilters.[i].gameObject
}
You reference the spheres and apply them to whatever you want.
As to your reply "So on my game screen I want to display a text that shows how many spheres were generated.", I gave that to you here
Debug.Log("Number of Spheres Found = " + meshFilters.Count);
So I did answer your question.
If you had code something like this
public Text numberOfSpheresText;
then ins$$anonymous$$d of the Debug statement you may want to do something like this
numberOfSpheresText.text = "Number of Spheres Found = " + meshFilters.Count;
You can change variable names. You can change the text.
But I have really given you everything you asked for with such little information provided.
Remember UA is a place to get help and that has been provided. I would appreciate if you would reward this answer as the correct answer.
Your answer
![](https://koobas.hobune.stream/wayback/20220612083752im_/https://answers.unity.com/themes/thub/images/avi.jpg)