Here's a great little tip for getting more out of your object names and simplifying development. With some Cunning Moxie, you can embed control data into the gameObject names to be used at startup, giving similar functionality to the object's 'tag'. In short, automated object setup controlled with object name.
Here's a screenshot with a script that needs access to a bunch of text objects to adjust the contents (in this case, to translate them based on a key).
Here's a screenshot with a script that needs access to a bunch of text objects to adjust the contents (in this case, to translate them based on a key).
Every processed text object needs a field in the script and a reference to the key in code. It's the kind of messy solution that grew from quick additions that you eventually question, "there must be a better way than this!" One alternative is to have a separate script on each object that controls translation, but that means adding a script to each target object and putting in the key (or other variable).
Instead, we can use Unity's FindObjectsOfType() method to find all the Text objects. But then how do we know if the string needs translating or not? After all, number strings don't need processing? Simply name the object with a prefix to control its processing! So name your objects that need translating "trans_" and then the key name. eg. "trans_UI_ClubNew" and "trans_match_difficulty".
Then use this code in your Start() call...
This runs through all the GameObjects in the scene and finds all the Texts. Then it tests each object to see if there's a "trans" prefix. If so, the rest of the name is used as the key for the translation process. Now all you need do when creating your text objects is name them with their contents and a control code.
It means one single point to change your data, the object name, and no need for additional script components or object references, and a very simple automated processing that updates as the scene changes. Although used here for translation, the true beauty of this is you can use it for anything, just like tags; simply put a suitable prefix in front of any object that needs processing. You could even have a separate substring for multiple control values if you want.
Important tip though: it's slow so only do it at initialisation. If you need to reference the objects in realtime, cache the search results (store the results list and reference that).
Got any good ideas for extending this concept? Let me know in the comments!
It means one single point to change your data, the object name, and no need for additional script components or object references, and a very simple automated processing that updates as the scene changes. Although used here for translation, the true beauty of this is you can use it for anything, just like tags; simply put a suitable prefix in front of any object that needs processing. You could even have a separate substring for multiple control values if you want.
Important tip though: it's slow so only do it at initialisation. If you need to reference the objects in realtime, cache the search results (store the results list and reference that).
Got any good ideas for extending this concept? Let me know in the comments!