Magic strings in Unity 3D
I've been slowly dying inside whenever I see a tag referenced in C# script examples using a magic string, e.g. "Enemy". I wonder what would happen if you decided to refactor and you then wanted to change "Enemy" to "NPCs"?
I personally would want to create an enum with some backing string value, so that if I want to rename my layer, I only have to change it in 2 places, instead of however many places it's used.
How do you deal with this?
I personally would want to create an enum with some backing string value, so that if I want to rename my layer, I only have to change it in 2 places, instead of however many places it's used.
How do you deal with this?
Comments
We avoid searching by name and by tag at all cost. We would usually design some sort of object pool to track what is what.
In some cases in unavoidable, i.e. layers, but that can be managed I guess by storing the bit that the layer references.