Save chosen language

I want to add multiple languages in my Game, so that the user can chose in the main menu his prefered language. The user already can chose a supported language and everything works fine, I just dont know how to save the chosen language over scenes or even forever. Right now the user have to chose the language in every single scene.

I heard from PlayerPrefs, but I really dont know how to save a language there.

1 Answer

1

This sort of thing is pretty much exactly what PlayerPrefs was designed for. I assume you have some sort of value that identifies which language the player selected from the menu? That’s the value you would save in PlayerPrefs.

I would create a function that enables the selected language:

void SelectLanguage(int languageId)
{
  // do the stuff to enable the language based on the language id
}

When the user selects a language from the menu…

int languageId = the id the user selected from the menu
SelectLanaguage(languageId)
PlayerPrefs.SetInt("Language", languageId);

And on startup you need to also enable that language

void Start()   // one some game initialization object
{
    int langageId = PlayerPrefs.GetInt("Language");
    SelectLanguage(languageId);
}

This is mostly pseudo-code so it may or may not work as is. The point is, create a single function that enables the appropriate language, and call that same function from both the menu when the user changes the languages, as well as during startup.