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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Editing Local Variables in runtime

Discussion in 'Localization Tools' started by emredesu, May 19, 2022.

  1. emredesu

    emredesu

    Joined:
    Aug 12, 2021
    Posts:
    51
    How can I access and edit the "Local Variables" of a Localize String Event from a script? I want to edit the gold cost of a chest during runtime using a value from a script, but I couldn't find how to do so.

     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,876
  3. emredesu

    emredesu

    Joined:
    Aug 12, 2021
    Posts:
    51
    Thank you very much for the lightning fast answer, and I've seen this documentation on how to retrieve local variables, however it doesn't explain how to set them. How can I initialize an IVariable so I can assign it when doing
    localizedString["chestName"] = "Musical Chest"
    ? Currently I get a
     "Cannot implicitly convert type 'string' to 'UnityEngine.LocaIization.SmartFormat.PersistentVariabIes.IVariabIe'"
    error.
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,876
    You need to cast it to the concrete type. One of these https://docs.unity3d.com/Packages/c...lization.SmartFormat.PersistentVariables.html

    In this case it would be a StringVariable.

    Code (csharp):
    1. (localizedString["chestName"] as StringVariable).Value = "Musical Chest"
     
    c-Row and emredesu like this.
  5. emredesu

    emredesu

    Joined:
    Aug 12, 2021
    Posts:
    51
    karl_jones likes this.
  6. Rangeroliver

    Rangeroliver

    Joined:
    Mar 13, 2017
    Posts:
    4
    I was trying to follow the steps above to acces "Local Variables", in my script I cant use "StringVariable" or "IntVariable", using UnityEngine.Localization library. I couldnt use the examples above.
     
  7. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,876
    Can you provide more details? Are you getting errors? What does your code look like?
     
  8. Rangeroliver

    Rangeroliver

    Joined:
    Mar 13, 2017
    Posts:
    4

    The name "Leon" appear in the text, but i want to change to "Leonard" in Start.
    I learning, some things is hard to get.
     

    Attached Files:

  9. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,876
    That should work. Looks like you are just missing a using statement, Visual Studio should be able to fix this automatically if you hover over the error. You need
    using UnityEngine.Localization.SmartFormat.PersistentVariables;
     
  10. Rangeroliver

    Rangeroliver

    Joined:
    Mar 13, 2017
    Posts:
    4
    Could not change the "Local Variables" yet, when I press play apeears "Leon" in the text, but when I press "P" nothing happens, should change to "Leonard".
    Is correct my localizedString reference?
     

    Attached Files:

  11. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,876
    Is the PlayerData script connected to a GameObject? The screenshot shows a LocalizedStringEvent, not a PlayerData.
    Try adding the script to the same GameObject and do something like

    Code (csharp):
    1. void Start()
    2. {
    3.     localizedString = GetComponent<LocalizedStringEvent>().StringReference;
    4. }
    Now you should reference the same localized string.
     
  12. Rangeroliver

    Rangeroliver

    Joined:
    Mar 13, 2017
    Posts:
    4

    Thank u so much! I forgot the reference =D
     
    karl_jones likes this.
  13. maikkanerva

    maikkanerva

    Joined:
    Dec 14, 2018
    Posts:
    23
    Is it possible to retrieve a list of local variables, like a List<(string, IVariable)>?
    What I'd like to do is to "clone" a localized string instace and copy the variables too.
     
  14. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,876
    maikkanerva likes this.
  15. maikkanerva

    maikkanerva

    Joined:
    Dec 14, 2018
    Posts:
    23
    karl_jones likes this.