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

Question Trouble with a Static Int used as a variable in an Argument for a Smart String

Discussion in 'Localization Tools' started by Shronky, Sep 29, 2023.

  1. Shronky

    Shronky

    Joined:
    Feb 12, 2018
    Posts:
    4
    Hello, I'm having problems with a Static Int, with a {get; set;} , that I'm using to save a highscore, and then using that Static Int when trying to update a Smart String (with said score).

    I have a script that accepts a normal int value, and it works, but when I try to assign my static int, it no longer works.

    Is this is a limit of localisation or of static ints? I've looked into converting a static int to an int (because I just need this script tor read that score, not change it), but the posts I've seen are not relevant, or speak of changing the value.

    Here's an example of my code below.
    Code (CSharp):
    1. public static HighscoreScript Instance { get; private set; }
    2.     [SerializeField] public LocalizedString localStringScore;
    3.     [SerializeField] public Text highscoreText;
    4.     private int highscoreNumber;
    5.  
    6.  
    7.  
    8.     void Start()
    9.     {
    10.         Instance = this;
    11.         highscoreNumber = SaveStore.Highscore ;
    12.         UpdateHighscoreText();
    13.     }
    14.  
    15.     private void OnEnable()
    16.     {
    17.         localStringScore.Arguments = new object[] { highscoreNumber };
    18.         localStringScore.StringChanged += UpdateText;
    19.     }
    20.     private void OnDisable()
    21.     {
    22.         localStringScore.StringChanged -= UpdateText;
    23.     }
    24.  
    25.     private void UpdateText(string value)
    26.     {
    27.         highscoreText.text = value;
    28.     }
    29.     public void UpdateHighscoreText()
    30.     {
    31.         localStringScore.Arguments[0] = highscoreNumber;
    32.         localStringScore.RefreshString();
    33.  
    34.     }
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    Hi,

    What do you mean it does not work?
    If the value of score is changing after Start then you will need to update your arguments, integers are value types so changing SaveStore.Hightscore will not update the value of highscoreNumber, you will need to copy the change across and also copy it across to Arguments.
    You may find it easier to use a persistent variable to store your score, you can then update that and it will trigger all localized strings that are using it to update themselves.

    https://docs.unity3d.com/Packages/c...istent-Variables-Source.html#global-variables
    https://docs.unity3d.com/Packages/c...ables-Source.html#custom-persistent-variables

    Take a look at the localization package samples, we have some that use persistent variables.
     
    Shronky likes this.
  3. Shronky

    Shronky

    Joined:
    Feb 12, 2018
    Posts:
    4

    Hi Karl,

    Thanks for the quick response!

    When I said it didn't work, you were correct in assuming that the value was being changed after Start. I had a previous line that loaded the value from PlayerPrefs, so I thought that should have loaded.

    For now, I've moved the definition into public void UpdateHighscoreText() and it now works perfectly (updates the values and the string).

    I'll also take a look at the packages you've linked too.

    Thanks again for your help!
     
    karl_jones likes this.