SelectedLocale is null. Database could not get table

Unity version 6 (6000.0.23f1, LTS). I just updated from the previous version to this one. When I exit play mode in my game, or when I make any changes in the UI Builder, I get three errors:

System.Exception: SelectedLocale is null. Database could not get table.
UnityEngine.Localization.Settings.LocalizedStringDatabase:GetLocalizedString (UnityEngine.Localization.Tables.TableEntryReference,UnityEngine.Localization.Locale,UnityEngine.Localization.Settings.FallbackBehavior,object[])
LocalizedTextUI:<.ctor>b__2_0 () (at Assets/Scripts/UI/LocalizedTextUI.cs:18)
UnityEngine.UIElements.UIElementsRuntimeUtilityNative:UpdatePanels ()

OperationException : SelectedLocale is null. Could not get table entry.

UnityEngine.Localization.Settings.LocalizedStringDatabase:GetLocalizedString (UnityEngine.Localization.Tables.TableEntryReference,UnityEngine.Localization.Locale,UnityEngine.Localization.Settings.FallbackBehavior,object[])
LocalizedTextUI:<.ctor>b__2_0 () (at Assets/Scripts/UI/LocalizedTextUI.cs:18)
UnityEngine.UIElements.UIElementsRuntimeUtilityNative:UpdatePanels ()

OperationException : SelectedLocale is null. Could not get localized string.

UnityEngine.Localization.Settings.LocalizedStringDatabase:GetLocalizedString (UnityEngine.Localization.Tables.TableEntryReference,UnityEngine.Localization.Locale,UnityEngine.Localization.Settings.FallbackBehavior,object[])
LocalizedTextUI:<.ctor>b__2_0 () (at Assets/Scripts/UI/LocalizedTextUI.cs:18)
UnityEngine.UIElements.UIElementsRuntimeUtilityNative:UpdatePanels ()

The localization on my text still works as expected. I have one localization table called main(StringTable). The selected collection is main(StringTable)(StringTable). I only have a string database, not an asset database.

My localization settings:

I’m using the UI Toolkit. My UI has just one instance of LocalizedTextUI. Here is that script:

using UnityEngine;
using UnityEngine.Localization.Settings;
using UnityEngine.UIElements;

[UxmlElement]
public partial class LocalizedTextUI : TextElement
{
    public static BindingId keyProperty = nameof(key);

    [UxmlAttribute]
    public string key;

    public LocalizedTextUI()
    {
        this.schedule.Execute(() => {
            if (!string.IsNullOrEmpty(key))
            {
                string loc = LocalizationSettings.StringDatabase.GetLocalizedString(key);

                if (!string.IsNullOrEmpty(loc))
                {
                    text = loc;
                    return;
                }

                if (Application.isPlaying)
                {
                    Debug.LogWarning("Text has no localized string");
                }

                text = key;
            }
        });
    }
}

Sounds like it could be an issue with calling Get LocalizedString whilst coming out of play mode.

Are you on the latest version of the package 1.5.3?
You may need to manually update by changing the manifest.json file in the project Package folder. If that doesn’t work please file a bug report.

Yes, I’m on the latest version, 1.5.3. The version in manifest.json is correct.

When in playmode we initialize the localization settings and pick a selected locale. We intentionally dont do this outside of playmode, the selected locale must be set manually if you want to preview a language. So calling GetLocalizedString outside of playmode when there is no selected locale will result in this error. You could put the call inside of Application.isPlaying to be safe or if you do want it to run outside of playmode you could check of LocalizationSettings.SelectedLocale is null before calling GetLocalizedString.
Alternatively you could use the localization bindings which handle all of this for you. I can see you are doing this in another thread so maybe you moved on from this problem :slightly_smiling_face:

Thank you for the response! I did consider using Application.isPlaying, but I forgot about it and wasn’t encountering these errors anymore. Thanks again for the responses in both threads!