Search Unity

Bug LocalizedString on ScriptableObject changing when saving prefab

Discussion in 'Localization Tools' started by PedroMarangon, May 30, 2023.

  1. PedroMarangon

    PedroMarangon

    Joined:
    Feb 27, 2018
    Posts:
    9
    [EDIT] I've managed to find out what was the problem, it was because the prefab had a default value and I was passing the reference of the localized name. Now I've changed (both in editor as well as at runtime, just to be sure) so that it creates a new LocalizedString while passing the original's TableEntryReference and TableEntryKeyReference:

    Code (CSharp):
    1. protected override void OnValidate()
    2.         {
    3.             base.OnValidate();
    4.             if (Application.isPlaying || abilityDetails == null) return;
    5.  
    6.             if (costImage != null) costImage.color = abilityDetails.ItemColor;
    7.  
    8.             if (localizedAbilityText != null)
    9.                 localizedAbilityText.StringReference = new LocalizedString(abilityDetails.Name.TableReference,abilityDetails.Name.TableEntryReference);
    10.  
    11.             if (costTMP != null) costTMP.text = $"{abilityDetails.SlotCount}";
    12.         }
    ------------------------------------------------

    Hi. I'm working on my metroidvania game and I'm using Scriptable Objects for detailing all information related to each ability (icon, main color, name, description, etc.). Naturally, since I'm posting on this forum, I'm having problems with the part of localization.

    I have a separate table that only contains the name and description of the abilities. However, when I open a prefab that references that SO (the prefab is a button that lets you toggle on and off the ability), for some reason the localized name in the SO get's reset to the second entry in the table (which is the name of the first ability).

    It's getting very annoying to update each ability to the correct name every time I update the prefab button (which I'm currently tweaking to be better looking).

    What's even weirder is that I don't have any way to change the value via code (in fact, I even changed the variable to be a private field with a get-only property and it's still changing the value).

    Here's the code for the SO and the button, respectively (i removed the parts unnecessary for you to see):

    ABILITY DETAILS SCRIPTABLE OBJECT:
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.Localization;
    4.  
    5. namespace Faunamorph.Player.Abilities
    6. {
    7.     [CreateAssetMenu(menuName = "Game/Abilities/Details")]
    8.     public class AbilityDetailsSO  : ScriptableObject
    9.     {
    10.         [field: SerializeField] public Sprite ItemSprite { get; private set; }
    11.         [SerializeField] private LocalizedString abilityName;
    12.         [field: SerializeField] public Color ItemColor { get; private set; }
    13.         [field: SerializeField] public Ability Ability { get; private set; }
    14.         [field: SerializeField] public LocalizedString Description { get; private set; }
    15.         [field: SerializeField] public int SlotCount { get; private set; } = 1;
    16.  
    17.         public LocalizedString Name => abilityName;
    18.     }
    19. }
    PREFAB BUTTON:
    Code (CSharp):
    1.  
    2. using Faunamorph.Player.Abilities;
    3. using System.Collections.Generic;
    4. using TMPro;
    5. using UnityEngine;
    6. using UnityEngine.EventSystems;
    7. using UnityEngine.Localization;
    8. using UnityEngine.Localization.Components;
    9. using UnityEngine.UI;
    10. using Utils;
    11.  
    12. namespace Faunamorph
    13. {
    14.     public class PauseAbilityButton : Selectable, ISubmitHandler, IPointerClickHandler
    15.     {
    16.         [SerializeField] private Color availableColor = Color.white;
    17.         [SerializeField] private Color equippedColor = Color.white;
    18.         [SerializeField] private Color unavailableColor = Color.white;
    19.         [SerializeField] private Image backgroundImage;
    20.         [SerializeField] private Image costImage;
    21.         [SerializeField] private TMP_Text costTMP;
    22.         [SerializeField] private LocalizeStringEvent localizedAbilityText;
    23.         [SerializeField] private LocalizedString unknownAbilityText;
    24.         [SerializeField] private AbilityDetailsSO abilityDetails;
    25.  
    26. #if UNITY_EDITOR
    27.         protected override void OnValidate()
    28.         {
    29.             base.OnValidate();
    30.             if (Application.isPlaying || abilityDetails == null) return;
    31.  
    32.             if (costImage != null) costImage.color = abilityDetails.ItemColor;
    33.             if (localizedAbilityText != null) localizedAbilityText.StringReference = abilityDetails.Name;
    34.             if (costTMP != null) costTMP.text = $"{abilityDetails.SlotCount}";
    35.         }
    36. #endif
    37.  
    38.         public void UpdateActiveAbilities(List<Ability> activeAbilities)
    39.         {
    40.             PlayerAbilities playerAbilities = PlayerRefs.Abilities;
    41.             Ability ability = abilityDetails.Ability;
    42.          
    43.  
    44.             bool hasAbility = playerAbilities.DoesHaveAbility(ability);
    45.             localizedAbilityText.StringReference = hasAbility ? abilityDetails.Name : unknownAbilityText;
    46.             costTMP.text = hasAbility ? abilityDetails.SlotCount.ToString() : "?";
    47.             costImage.color = hasAbility ? abilityDetails.ItemColor : Color.clear;
    48.  
    49.             bool doesNotHaveSpace = !playerAbilities.CanEquipAbility(ability);
    50.             bool abilityIsEquipped = playerAbilities.CanUseAbility(ability);
    51.  
    52.             backgroundImage.color = abilityIsEquipped ? equippedColor : (doesNotHaveSpace ? unavailableColor : availableColor);
    53.         }
    54.     }
    55. }
     
    Last edited: May 30, 2023
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281
    Sounds like you got it working. :)