Best way to create custom attributes?
One of the most frequently needed things is information about objects the player collides with. Typically some kind of attribute would help to indicate that the object we collided with is breakable or walkable or something.
What is the best way to do this? Creating a script to attach to these objects for the sole purpose of containing and returning a constant seems utterly excessive. I am wondering if there is a different way to assign custom attributes to a gameObject.
And, no, Tag will not work because I will need more than one attribute.
Answer by aditya · Feb 17, 2017 at 05:52 AM
of-course Tags can help you in this, lets understand this with an example, say we have a pillar that is breakable then set its tag like Pillar_Breakable, then your script will look something like this
if(collidedObject.tag.Contains("Pillar")){ // First check if we have a collision with pillar
if(collidedObject.tag.Contains("Breakable")){ // Now check if this pillar is breakable
// Break this pillar
}
}
Please don't just copy paste above code as it is Untested
Answer by StridingDragon · Feb 17, 2017 at 06:18 AM
What if I need the pillar to be breakable and climbable? Tags are completely useless for that purpose, because every gameObject can contain only one Tag. Unless you want to create a million different tags to represent all possible combinations of attributes. Not to mention that all these string-based tags are not exactly efficient.
What Unity should really offer is a 64-bit custom value that is part of every gameObject. That way we could create bit flags and attach them directly to any object, retrieving them very quickly. I am really surprised this does not exist.
Your answer
![](https://koobas.hobune.stream/wayback/20220612111306im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Put a GameObject in the script's inspector when the script belongs to a prefab 1 Answer
How to create texture at runtime (Without using TextureImporter)? 1 Answer
Is there a way to pass instance values to a custom PropertyAttribute? 1 Answer
Get velocity of instantiated Prefab 0 Answers
How do you make a SyncList gather data from before the user was connected? 0 Answers