Tags VS Layers to find the number of a type of objects in the current level

I understand that Layers are used for determining which objects are raycast to, which get renderered, lit, and which objects collide together and Tags are intended to identify GameObjects for scripting purposes.

I have a bunch of collectables in my levels but they have different Tags (“collectableTypeA”, “collectableTypeB” etc, there are 6 different types of them). I want to know each moment, how many collectables (not of a specific type, I want the total number) I’ve got left.

I was thinking that (since they have different Tags) if I had the collectables in a separate layer, that there might be a way to get the number of objects in that layer, at every moment. If not, I will probably try to get the sum by adding objects with the aforementioned Tags (I think by using “GameObject.FindGameObjectsWithTag”).

I just wanted to know if there is a way by using Layers, because if there is, it will be probably easier and in any case it would be useful to know.

If there isn’t though (by using Layers), is the way I mentioned above the right one (that is, by finding the objects with various tags and adding them inside a variable)?

you could maybe just add all your collectable in a list from a start fct or awake, so you populate your list with them then this is easy to keep track

or in case your collectable have a specific class for them like Collectable or Item class then you can just use a find object typeof fct to get all your object count in the scene

I was thinking of creating a list and using list.Count to get the length of it every moment.
I think now (since I have no knowledge of C#) I will work with an Array: I’ll create it at the beginning of the Level (from inside function Awake) and put the Array.length in a variable. Then, while the Level plays, whenever a collectible is collected (hence destroyed) the variable will be reduced by 1 and so on, till it reaches 0. Then I’ll load the next level.
About the Object.FindObjectOfType use, I understand that it is very heavy for the processor, but since I’ll use it only once at the beginning of the Level I don’t think it will pose a problem.
Have a good day Giyomu :slight_smile: