Call a function on all objects

Hello,

I have a language file I’m using for storing all the strings in my game, it works fine.

Thing is, when I change the language in the options, I have an update function in the file reader that reads the data for the appropriate language, and I need all the text objects of the UI to update to the new language.

I don’t want to have every text object continuously check if the language has changed in Update(), so is there a way to call an UpdateText() function on all text objects when the language is changed in the options ?

I was thinking of tagging all the text elements, then get all elements by tag, and calling the function from there. Is this an optimal way of proceeding ?

UI elements with text components should all be of type Text, so import UnityEngine.UI and use:
Text textObjects = GetComponents()
to get ahold of all of them. You can then loop through the array changing their text via:
textObjects*.text = “new text”*
where “new text” is whatever their new text should be (you’ll probably be looking at their current text and looking up the translation in your language file.)
GetComponents won’t return any objects that are disabled in the scene, so you’ll need to leave your entire UI enabled, populate the array of Text objects in an Awake or Start block, then disable the appropriate UI canvases, panels, etc., via script.