
The biggest problem with tags is they tend to promote a liberal use of FindGameObjectsWithTag, and that should only be used in one off situations, not every update.We are facing a strange crash on iOS 16.4 and 16.4.1. This is because objects are instantiated and spawned server side, but on the client side the instantiation of objects is in response to received network messages instead of in a place where object references can be directly cached from the appropriate script. I have some use of tags on the client side of my networked game to establish references between objects.
Unity forum code tags code#
Though I often go back through my code later and set inspector references instead.

Unity forum code tags windows#
My various UI windows sometimes need references to scripts on other UI windows, so I have a base UI object with references that branch out to every other UI window which I just tag with UI for an easy FindWithTag if needed.

If it hit a Ship tagged object I do something to the ship, otherwise I don't. In OnCollisionEnter using CompareTag I check what kind of object a projectile hits. So, if you're having performance issues due to needing to find and act on lots of stuff in the scene, you might consider Hybrid ECS as an approach. It turns out that this is fairly similar (I believe) to the approach that Hybrid ECS uses to track things performantly in the scene. Then my code can iterate over the arrays in my tracking service rather than finding the objects through Unity's methods. Instead, for each object I need to track in this way, I add a component that causes it to register itself with a singleton tracking service on Awake, and to unregister itself on destroy. Using approaches like FindGameObjectsWithTag or FindObjectsOfType is much too slow for that. But I try to avoid that if possible.Īs for your other question of "finding" stuff in your scene, I have various use cases where I need to act on all the stuff in the scene every frame, or every fixed update. Occasionally I'll check the tags when doing some raycast in cases where the limited number of layers prevents me from being able to have a dedicated layer for all combinations of traits.Similarly, for bullet impact sounds and decals, the surface type a bullet hits determines what kind of sound and decal to use.I went with tags for this, instead of layers, because there is a lot more variety of surface types than layers would support.

Each tag maps back to a "surface type" that determines what kinds of sounds to play.
