Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Player prefs not working unity 2019.4(LTS) Mobile

Discussion in 'Editor & General Support' started by FireJojoBoy, Aug 20, 2020.

  1. FireJojoBoy

    FireJojoBoy

    Joined:
    Oct 30, 2019
    Posts:
    65
    Hello, im trying to translate my TMP text with the following script:
    Code (CSharp):
    1. using UnityEngine;
    2. using TMPro;
    3.  
    4. public class Translation : MonoBehaviour
    5. {
    6.     public string de = "german text";
    7.     public string en = "english text";
    8.     public bool UpdateLang = false;
    9.     private TextMeshProUGUI text;
    10.  
    11.     void Awake()
    12.     {
    13.         text = gameObject.GetComponent<TextMeshProUGUI>();
    14.         //Translate text
    15.         if (PlayerPrefs.GetString("Lang", "en") == "en")
    16.         {
    17.             text.text = en;
    18.         }
    19.         else if (PlayerPrefs.GetString("Lang", "en") == "de")
    20.         {
    21.             text.text = de;
    22.         }
    23.     }
    24.  
    25.     void FixedUpdate()
    26.     {
    27.         if (UpdateLang)
    28.         {
    29.             //Translate text
    30.             if (PlayerPrefs.GetString("Lang", "en") == "de")
    31.             {
    32.                 text.text = de;
    33.                 Debug.Log("german works now");
    34.             }
    35.             else if (PlayerPrefs.GetString("Lang", "en") == "en")
    36.             {
    37.                 text.text = en;
    38.             }
    39.         }
    40.     }
    41. }
    when i press my button to change the langruige this script gets called and it seems to work:

    Code (CSharp):
    1. public void ChageLang()
    2.     {
    3.         if (PlayerPrefs.GetString("lang", "en") == "en")
    4.         {
    5.             PlayerPrefs.SetString("lang", "de");
    6.         }
    7.         else if (PlayerPrefs.GetString("lang", "en") == "de")
    8.         {
    9.             PlayerPrefs.SetString("lang", "en");
    10.         }
    11.         Debug.Log(PlayerPrefs.GetString("lang"));
    12.     }
    but the scribt above allways thinks the Langruige is english

    EDIT: i typed "Lang" instead of "lang"
     
    Last edited: Aug 20, 2020
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    I highly suggest creating a string constant so you avoid these errors in the future:

    Code (CSharp):
    1. const string LANGUAGE_KEY = "lang";
    2.  
    3. // Then later on use that key everywhere:
    4. PlayerPrefs.GetString(LANGUAGE_KEY...)
    5. PlayerPrefs.SetString(LANGUAGE_KEY...)
    This way if you misspell something, the compiler will give you an error.
     
  3. FireJojoBoy

    FireJojoBoy

    Joined:
    Oct 30, 2019
    Posts:
    65
    thanks :D