- Home /
GameObject[0].SetActive (false); more than one GamObject possible?
I want to make a game in 2 different languages, i had an idea how to do it but there are too many texts to write it like this: englishTexts[0].SetActive (true); germanTexts[0].SetActive (false);
englishTexts[1].SetActive (true); germanTexts[1].SetActive (false); ..... and it goes on like this, i already have 15 Texts and there are coming more. So it is possible to somehow put multiple Objects (Texts) in one? Example: englishTexts[0, 1, 2].SetActive (true); Hope you can help, thanks public GameObject mainMenuHolder; public GameObject achievementsMenuHolder; public GameObject optionsMenuHolder; public GameObject languagesHolder; public GameObject[] englishTexts; public GameObject[] germanTexts;
void Update() {
if (PlayerPrefs.GetString("english") == "true") {
englishTexts[0].SetActive (true);
germanTexts[0].SetActive (false);
}
if (PlayerPrefs.GetString("german") == "true") {
englishTexts[0].SetActive (false);
germanTexts[0].SetActive (true);
}
}
Answer by Bunny83 · Nov 21, 2017 at 11:13 PM
Just use a loop. Also it makes no sense to use two seperate player prefs for choosing the language. It makes more sense to use a "language" string and set it to the language you want.
In addition you shouldn't execute this every frame. Using a seperate method to update the language would make more sense. Call it once in Start and whenever you actually changed the language
void UpdateTexts()
{
string lang = PlayerPrefs.GetString("language");
for(int i = 0; i < englishTexts.Length; i++)
{
englishTexts[i].SetActive (lang == "english");
germanTexts[i].SetActive (lang == "german");
}
}
This will enable all english texts when "lang" is "english" and also disable all the german texts. This of course assumes that you have the same amount of texts in each array. If for some reason you don't have the same amount you need to use seperate loops
void UpdateTexts()
{
string lang = PlayerPrefs.GetString("language");
for(int i = 0; i < englishTexts.Length; i++)
{
englishTexts[i].SetActive (lang == "english");
}
for(int i = 0; i < germanTexts.Length; i++)
{
germanTexts[i].SetActive (lang == "german");
}
}
Thanks for your answer, this is exactly what i need. I will try it out when i have time in a few hours
If i put in "void UpdateTexts" it doesn't work, if i change it to "void Update" it does work. Could you explain me what's the difference? :/
Have you missed this paragraph in my answer:
In addition you shouldn't execute this every frame. Using a seperate method to update the language would make more sense. Call it once in Start and whenever you actually changed the language
Okay so i changed your script a little bit and now it works perfectly and it's easy to reconstruct. Thank you for the help
Answer by voncarp · Nov 21, 2017 at 11:09 PM
If you want to hardcode it in, something like this works if you can follow it. You just send the string from the text. And if it matches and you have your preferred language selected it should return the translated value. You might need to mod it a bit to get what you want.
using UnityEngine;
public class localize : MonoBehaviour {
public enum language {
english,
vietnamese
}
public language currentLanguage;
public static language l;
void Awake() {
switch (currentLanguage) {
case language.english:
l = language.english;
break;
case language.vietnamese:
l = language.vietnamese;
break;
}
}
public static string translate (string txt) {
switch (txt) {
case "PICK UP ":
switch (l) {
case language.english:
txt = "PICK UP ";
break;
case language.vietnamese:
txt = "chọn ";
break;
}
break;
case "WATER BAG":
switch (l) {
case language.english:
txt = "WATER BAG";
break;
case language.vietnamese:
txt = "Túi nước";
break;
}
break;
}
return txt.ToUpper();
}
}
Your answer
Follow this Question
Related Questions
OnMouseDown() function is not changing gameobject declared outside. 1 Answer
comparing variables of gameobjects in an array 2 Answers
same script on multiple objects access to different variables from another script 0 Answers
cant set object in prefab object, but can in the scene 1 Answer
Finding the Sum of Values of Multiple GameObjects in an Array + Variable Sized arrays 0 Answers