- Home /
Is there an alternative to Tags in unity?
Dear comm(Unity), Right now I have a particlesystem that lights fire to everything it touches (like trees, crates, etc) as long as the object is tagged as "Flammable" (I tag flammable objects as "Flammable" and put a FlammableObjectScript.cs on them. Something like this:
OnParticleCollision (GameObject other)
if (other.tag == "Flammable"){
other.GetComponent<FlammableObjectScript>().onFire = true;}
It works great, BUT there is one caveat. I have some breakable wooden and metal crates from Unity asset store which are tagged "crushObj". If I take the tag off them, I'm pretty sure the "Breaking" animation will no longer work on them, and Unity does not support multiple tags. Does anyone know a better alternative to tags which I can use to check if an object should be flammable or have the script I'm looking for?
Answer by Hellium · Jan 18, 2018 at 11:27 PM
Just check if the FlammableObjectScript
is attached to the gameobject and put away the Flammable
tag:
OnParticleCollision (GameObject other)
{
FlammableObjectScript flammableScript = other.GetComponent<FlammableObjectScript>() ;
if (flammableScript != null)
flammableScript.onFire = true;
}
Additional advantage : you don't need to remember tagging the object.
Awesome!! Thank you so much!! That should work. Forgive me as I'm pretty new to coding and haven't even learned about null yet other than seen it mentioned here and there. Definitely have never seen it used to check if a component was on a GameObject but that makes total sense. Surprised I haven't seen it more
Check the Spacepuppy framework for a sexy multi-tag system. There are many things you need to do in order to support this though, like extension methods on GameObjects and $$anonymous$$onoBehaviours, a ScriptableObject to hold the collective tag data itself in the manner of a singleton asset, etc... You may want to read through and include the two primary assemblies as a whole (SpacepuppyUnityFramework and SpacepuppyUnityFrameworkEditor)- they have tons of useful classes and utilities.
Your answer
![](https://koobas.hobune.stream/wayback/20220612150814im_/https://answers.unity.com/themes/thub/images/avi.jpg)