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

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.

public static HighscoreScript Instance { get; private set; }
    [SerializeField] public LocalizedString localStringScore;
    [SerializeField] public Text highscoreText;
    private int highscoreNumber;



    void Start()
    {
        Instance = this;
        highscoreNumber = SaveStore.Highscore ;
        UpdateHighscoreText();
    }

    private void OnEnable()
    {
        localStringScore.Arguments = new object[] { highscoreNumber };
        localStringScore.StringChanged += UpdateText;
    }
    private void OnDisable()
    {
        localStringScore.StringChanged -= UpdateText;
    }

    private void UpdateText(string value)
    {
        highscoreText.text = value;
    }
    public void UpdateHighscoreText()
    {
        localStringScore.Arguments[0] = highscoreNumber;
        localStringScore.RefreshString();

    }

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/com.unity.localization@1.4/manual/Smart/Persistent-Variables-Source.html#global-variables
https://docs.unity3d.com/Packages/com.unity.localization@1.4/manual/Smart/Persistent-Variables-Source.html#custom-persistent-variables

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

1 Like

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!

1 Like