Localization Errors

Hi All,
I have been seeing this error for sometime but as it never caused any problems I ignored it until it started crashing builds. When I attempt to set smart variable values via:

profileTextIDVariable.Value = id;
profileTextProgressVariable.Value = ProfileManager.Instance.GetProgress(id);
var newGamePlusCount = ProfileManager.Instance.GetNewGamePlusCount(id); //GENERATES ERROR
profileTextNewGamePlusCountVariable.Value = newGamePlusCount <= 0 ? "" : $"+{newGamePlusCount}";

profileTextLocalizer.ApplyLocaleVariant(LocalizationSettings.SelectedLocale); //GENERATES ERROR

I get the following error:

NullReferenceException: Object reference not set to an instance of an object
  at UnityEngine.Localization.PropertyVariants.TrackedObjects.JsonSerializerTrackedObject.ApplyLocale (UnityEngine.Localization.Locale variantLocale, UnityEngine.Localization.Locale defaultLocale) [0x00147] in .\Library\PackageCache\com.unity.localization@1.5.2\Runtime\Property Variants\Tracked Objects\JsonSerializerTrackedObject.cs:169 
  at UnityEngine.Localization.PropertyVariants.GameObjectLocalizer.ApplyLocaleVariant (UnityEngine.Localization.Locale locale, UnityEngine.Localization.Locale fallback) [0x0007f] in .\Library\PackageCache\com.unity.localization@1.5.2\Runtime\Property Variants\GameObjectLocalizer.cs:197 
  at UnityEngine.Localization.PropertyVariants.GameObjectLocalizer.ApplyLocaleVariant (UnityEngine.Localization.Locale locale) [0x00000] in .\Library\PackageCache\com.unity.localization@1.5.2\Runtime\Property Variants\GameObjectLocalizer.cs:171 
  at UnityEngine.Localization.PropertyVariants.GameObjectLocalizer.RequestUpdate () [0x00042] in .\Library\PackageCache\com.unity.localization@1.5.2\Runtime\Property Variants\GameObjectLocalizer.cs:269 
  at UnityEngine.Localization.PropertyVariants.GameObjectLocalizer.<RegisterChanges>b__18_0 (System.String _) [0x00000] in .\Library\PackageCache\com.unity.localization@1.5.2\Runtime\Property Variants\GameObjectLocalizer.cs:224 
  at UnityEngine.Localization.LocalizedString.InvokeChangeHandler (System.String value) [0x00023] in .\Library\PackageCache\com.unity.localization@1.5.2\Runtime\Localized Reference\LocalizedString.cs:699 
UnityEngine.DebugLogHandler:Internal_LogException(Exception, Object)
UnityEngine.DebugLogHandler:LogException(Exception, Object)
UnityEngine.Logger:LogException(Exception, Object)
UnityEngine.Debug:LogException(Exception)
UnityEngine.Localization.LocalizedString:InvokeChangeHandler(String) (at .\Library\PackageCache\com.unity.localization@1.5.2\Runtime\Localized Reference\LocalizedString.cs:710)
UnityEngine.Localization.LocalizedString:RefreshString() (at .\Library\PackageCache\com.unity.localization@1.5.2\Runtime\Localized Reference\LocalizedString.cs:235)
UnityEngine.Localization.LocalizedString:OnVariableChanged(IVariable) (at .\Library\PackageCache\com.unity.localization@1.5.2\Runtime\Localized Reference\LocalizedString.cs:678)
UnityEngine.Localization.SmartFormat.PersistentVariables.Variable`1:SendValueChangedEvent() (at .\Library\PackageCache\com.unity.localization@1.5.2\Runtime\Smart Format\Extensions\Persistent Variables\PersistentVariables.cs:46)
UnityEngine.Localization.SmartFormat.PersistentVariables.Variable`1:set_Value(Int32) (at .\Library\PackageCache\com.unity.localization@1.5.2\Runtime\Smart Format\Extensions\Persistent Variables\PersistentVariables.cs:39)

I am also seeing some warnings which I assume is contributing to the problem:

Attempting to Apply Variant when the previous operation has not yet completed.
UnityEngine.StackTraceUtility:ExtractStackTrace () (at C:/build/output/unity/unity/Runtime/Export/Scripting/StackTrace.cs:37)
UnityEngine.DebugLogHandler:LogFormat (UnityEngine.LogType,UnityEngine.Object,string,object[])
UnityEngine.Logger:Log (UnityEngine.LogType,object,UnityEngine.Object)
UnityEngine.Debug:LogWarning (object,UnityEngine.Object)
UnityEngine.Localization.PropertyVariants.GameObjectLocalizer:ApplyLocaleVariant (UnityEngine.Localization.Locale,UnityEngine.Localization.Locale) (at ./Library/PackageCache/com.unity.localization@1.5.2/Runtime/Property Variants/GameObjectLocalizer.cs:185)
UnityEngine.Localization.PropertyVariants.GameObjectLocalizer:ApplyLocaleVariant (UnityEngine.Localization.Locale) (at ./Library/PackageCache/com.unity.localization@1.5.2/Runtime/Property Variants/GameObjectLocalizer.cs:171)
UnityEngine.Localization.PropertyVariants.GameObjectLocalizer:SelectedLocaleChanged (UnityEngine.Localization.Locale) (at ./Library/PackageCache/com.unity.localization@1.5.2/Runtime/Property Variants/GameObjectLocalizer.cs:107)
UnityEngine.Localization.PropertyVariants.GameObjectLocalizer/<Start>d__10:MoveNext () (at ./Library/PackageCache/com.unity.localization@1.5.2/Runtime/Property Variants/GameObjectLocalizer.cs:96)
UnityEngine.SetupCoroutine:InvokeMoveNext (System.Collections.IEnumerator,intptr) (at C:/build/output/unity/unity/Runtime/Export/Scripting/Coroutines.cs:17)

I have attempted to change async loading/sync loading, preloading options etc. but none seem to make a difference, any ideas?

Unity Editor 2022.3.46
Localization 1.5.2

Thanks!
Colton

Are you able to share the project or a sample project that has the same error?

Hey @karl_jones, thanks for the reply. Yes I have attached a sample project. The issue is caused by enabling disabled text and changing the values at the same time. Is there anyway I can force the GameObjectLocalizer to get the active locale?

Thanks!
Colton

My project.zip (1.4 MB)

Looks like you found a bug. I have created a bug report to track it here https://issuetracker.unity3d.com/product/unity/issues/guid/LOC-1152

You can workaround this now by making a small change to the package. You will need to move the package from the package cache into the projects Packages folder to make changes. Then edit the file Runtime\Property Variants\GameObjectLocalizer.cs at line 267.
Change the method to be this:

void RequestUpdate()
{
    // Ignore the change, it will be handled by the selected locale change event.
    if (m_IgnoreChange || m_CurrentLocale == null || LocalizationSettings.Instance.IsChangingSelectedLocale || (CurrentOperation.IsValid() && !CurrentOperation.IsDone))
        return;
    ApplyLocaleVariant(m_CurrentLocale);
}

I added an additonal null check m_CurrentLocale == null.
If you still get the other error let me know, it may be another issue we need to look into although the above fix will hopefully fix both errors.

Thanks for reporting the issue!