Search Unity

Question Scriptable Object Localization

Discussion in 'Localization Tools' started by Frayed_JDM, Jul 1, 2021.

  1. Frayed_JDM

    Frayed_JDM

    Joined:
    May 1, 2020
    Posts:
    5
    Hello,
    I’ve been using Unity for a little while now and have started to learn some programming, I thought it’d be a good idea to implement the localization tool and am ok with setting gameobjects up with the Localize String Event script linked to a TMP component etc, that all works perfectly.

    The issue I’m having and have been stuck on for a few days is accessing or referencing a localized string in a scriptable object:
    I’m making a little rpg and have setup the characters and monsters stats data as scriptable objects so that later on they can be loaded into a battle scene dynamically etc.

    Their names and attacks are localised within the scriptable objects (using LocalizedString), however I cannot reference the names from another script to load them into a Text Component without resorting back to just using a normal string, but then that means I cannot localize the name.

    Hope that makes sense.

    I am by no means a programmer, I’m learning as I go, but I don’t know if I’ve missed something glaringly obvious or whether what I’m trying to do is even possible.

    Experimenting and trying to figure out work arounds usually gets me errors along the lines of: cannot covert localized string into string or something like that.

    It’s quite possible it’s beyond the scope of my ability, but I thought I’d ask anyway. Many thanks!
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,297
    Could you show some example code of what you have?
     
  3. Frayed_JDM

    Frayed_JDM

    Joined:
    May 1, 2020
    Posts:
    5
    Hi thanks for the reply, I'll try to show what I mean:
    01.jpg
    This is the Scriptable Object 'CharacterBase' script and on the right is an example Character I've created using the scriptable object 'CharacterBase' the name of the character is a LocalizedString, in this case I've called him Barry and added a Japanese version.

    02.jpg
    The Scriptable Object Barry is referenced in the 'CharacterLevel' script with 'Basestats' this is where the data for his level, moves etc are all accessed - in order to load his details into a HUD where his name, level etc go into a TMP component and displayed on a GUI - are pulled from the Scriptable Object - the 'BattleHUD' script accesses this data by loading CharacterLevel - grabbing the Basestats reference which I'm guessing grabs the details like Localized name, HP, other stats etc

    If his name is for example just a normal public string I can type:

    nameText.text = characterLevel.Basestats.name

    - and because this is a normal text string, it loads into the HUD no problem - I cannot however do this for a LocalizedString - I don't know how to grab the LocalizedString of his name and load it into the HUD by pulling data from the Scriptable Object.

    I hope this makes sense, again I'm not a programmer and I'm learning as I go along, I kinda understand how the data is accessed, I just don't know how to access that Localized version of his name.

    I apologise in advance if I have formatted this reply incorrectly or over used the images.
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,297
    Try calling LocalizedString.GetString();
     
  5. Frayed_JDM

    Frayed_JDM

    Joined:
    May 1, 2020
    Posts:
    5
    I don't know if I'm putting the LocalizedString.GetString(); in the right place - I'm adding it to the:
    nameText.text = characterLevel.Basestats.name
    But I'm getting an error:

    error CS1061: 'CharacterBase' does not contain a definition for 'LocalizedString' and no accessible extension method 'LocalizedString' accepting a first argument of type 'CharacterBase' could be found (are you missing a using directive or an assembly reference?)
     
  6. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,297
    Looks like it should be
    characterLevel.Basestats.name.GetString()
     
  7. Frayed_JDM

    Frayed_JDM

    Joined:
    May 1, 2020
    Posts:
    5
    I still get error CS1061: 'string' does not contain a definition for 'GetString' and no accessible extension method 'GetString' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)

    It seems it can't grab 'LocalizedString name;' defined in the CharacterBase scriptable object -which allows me to enter the localized fields for the character's name, into the characterLevel.Basestats.name to display that localized name in a TMP component for the BattleHUD.
     
  8. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,297
    Sorry it should be GetLocalizedString. I was trying to answer on my phone before.
    I dont fully understand your script structure but I think you want this:
    Code (csharp):
    1. nameText.text = characterLevel.Basestats.Name.GetLocalizedString()
     
  9. Frayed_JDM

    Frayed_JDM

    Joined:
    May 1, 2020
    Posts:
    5
    Hi, that has done it! I've been going over this for the last 3 days with no joy and it was as simple as that! I had to change the LocalizedString field to public in the CharacterBase script (it gave me a 'doesn't have permission' type error) and I changed the 'name' part to 'title' instead because for some reason it was pulling the file name of the scriptable object?

    But otherwise it works and I can update it to display the localized text, thank you very much for your help and patience, I've got a long way to go with learning to program but I'm getting the handle on it.

    Again much appreciated, thank you! :)
     
    karl_jones likes this.
  10. Obay_naeem

    Obay_naeem

    Joined:
    Jan 22, 2020
    Posts:
    7
    hi guys
    i'm facing kinda the same problem Frayed_JDM was facing
    and i solved it as karl_jones describe
    the thing is i have a dialogue system
    and i'm using scriptable objects to store the texts and their time to animate on the UI
    but instead of entering the text from the inspector , i want to take a localized string from a "Dialogue Table"
    and assigned it to my dialogue scriptable object . Capture.PNG
    is this good way to handle localization in a dialogue system?
     
    Last edited: Oct 18, 2021
  11. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,297
    This won't ever update if the language is changed. Instead subscribe to StringChanged in OnEnable and use that callback to update the string array.
     
  12. Obay_naeem

    Obay_naeem

    Joined:
    Jan 22, 2020
    Posts:
    7
    thank you very much:)