I have these game objects in my scene and I want my script to calculate the number of them in the scene when it starts up so I can put it into a Gui Text. Is there a function to calculate the number of certain objects in the scene or in? Could you use a tag or layer and find 0ut how much is in that tag or layer. I just need the script please!
This should do it :
var objectCount : float;
var Display : GUIText;
function Update () {
objectCount = UnityStats.vboTotal;
Display.text = objectCount + " Objects";
}
1 Like
I only want to do it for one type of object ,but I’ll try and work with this.
Use this to find all active objects of Class SomeClassName or all active objects with tag “SomeTag”.
int numberOfClassObjects = GameObject.FindObjectsOfType(typeof(SomeClassName)).Length;
int numberOfTaggedObjects = GameObject.FindGameObjectsWithTag("SomeTag").Length;
GameObject.Find…() functions are slow. So don’t use it every frame!
6 Likes
why float? i don’t think there is going to be half of an object ever! right?
2 Likes
Why resurrect a 5 year old post to ask?
1 Like
This really brings back memories.
I was so new to Unity back then
5 Likes
And writing in UnityScript too. Those were the days.
Probably because the code was written on the fly on the internet. Expect minor errors and inefficiencies.
3 Likes
In case you might forget, this old post is still helpful