Hi, dont know if this is the right place to ask (or unity answers) but i was wondering for a while with this question: what is better in terms of performance? parenting or tagging?
I mean, usually you could simply group your items under the same gameObject and just look for its parent.
What benefit does tagging add?
They do very different things. Parenting affects a lot of different things, like position and message broadcasting, but tagging is its own system that doesn’t affect much else. To be honest I’ve never even thought of parenting as an alternative to tagging.
That said, I never use tagging, and I’ve never needed to. It’s a redundant system that is largely incompatible with large projects (e.g. because you can only ever have one tag on a given object). I’ve always considered MonoBehaviours to be far more versatile - add a MonoBehaviour rather than a tag and you can do FindObjectsOfType() (slow but easy) or maintain a list of them in the script itself (efficient). You can use GetComponentsInChildren() to get all the objects you’ve ‘tagged’ with a MonoBehaviour.
And you can use inheritance to make this even more useful. For example, using the tag system, you might have an Enemy tag. But what if you want to separate between enemy combatants and enemy medics? Suddenly you need more information. If you’re tagging with MonoBehaviours, you can just add a member variable to the Enemy class, OR, you can make classes EnemyCombatant and EnemyMedic that derive from Enemy. GetComponentsInChildren will return all of them, while GetComponentsInChildren will only return the combatants.
…
The truth is, the best thing to do is to organize your project in the way that makes sense to you. Performance is going to be effectively identical between the different methods you proposed, if that’s your main concern.
I agree with StarManta there. Neither will change performance enough to make any massive difference (Unless in VERY extreme events, which are unlikely). Normally it comes to a matter of ‘house cleaning’. The way that suits your project best.
Unity is specifically designed for indie projects which can be prototyped fast. The tag system was made with that in mind. Tag something, grab something by tag. Works fast and easy enough. As the project gets larger, you may want a more intricate system for all your ‘things’.
Well thats what i was thinking about.
In terms of “subclassing”, using parenting you can just have an “enemy container object” and then some “specific enemy container object”, then you can easily retrieve all enemies of a certain type or know which kind of enemy you are handling on a given moment. Seems way better than simply taggin and faster than searching.
Thank you very much for the responses and i’ll take a closer look to the methods Star Manta mentioned ![]()
Yeah there’s so so so many ways to do it, it’s really a matter of finding what’s right for the scope of your project ![]()
Good luck bud
In general, it’s probably best to consider a transform parent as a physical grouping, rather than a conceptual grouping. If for no other reason than, the hierarchy and parentage is fundamentally based on the Transform component, which is explicitly the component that describes the position in the world. Let’s say you want to group your characters as Enemy or Ally, and as “in room 1” and “in room 2” and “on platform 3”. It makes a lot more conceptual sense to reparent the characters based on which room they are in rather than by which team they’re on.
For example, for efficiency, if the player is in room 2 and all the doors are closed, you can disable the room 1 GameObject to optimize performance - all the allies and enemies in that room will stop being processed. If a platform is moving, it can move along with it any object on the platform. It’ll be more rare for similar situations to come up if they’re parent-grouped by ally or enemy.
This. Transforms are about physical location. Parenting is all about the physical relationship between transforms. So a characters arm should move relative to his body. A group of characters should move relative to the elevator they are in. A mesh should move with it’s character and so on.
You can often abuse this system and get some logical grouping. Like making all projectiles the child of an empty object to clean up the hierarchy. But I wouldn’t rely too heavily on this system.