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. Dismiss Notice

[Solved] NullReferenceException when using LocalizedString

Discussion in 'Localization Tools' started by DatThax, May 1, 2020.

  1. DatThax

    DatThax

    Joined:
    Jun 18, 2019
    Posts:
    10
    I'm trying to use LocalizedString in a script like so:

    Code (CSharp):
    1. public LocalizedString TEST;
    2.  
    3. private void OnEnable()
    4.     {
    5.         var t1 = TEST.GetLocalizedString();
    6.         if (t1.IsDone)
    7.         {
    8.             Debug.Log(t1.Result);
    9.         }
    10.     }
    I have assigned a string reference in the Inspector. This is the exact error I get:

    I appreciate any help and please tell me if I'm simply doing something wrong of it's a possible bug.

    Unity Version: 2019.3.9f1
    Localization: preview - 0.6.1
    Adressables: 1.8.3
     
  2. Alexis-Dev

    Alexis-Dev

    Joined:
    Apr 16, 2019
    Posts:
    121
    Hi,

    Did you initialized LocalizationSetting before call GetLocalizedString?

    Exemple with async function:
    Code (CSharp):
    1.  if(!LocalizationSettings.InitializationOperation.IsDone) await LocalizationSettings.InitializationOperation.Task;
    And did you Set a reference to LocalizedString?

    Code (CSharp):
    1. localizedString.SetReference(table, key);
    Best,
    Alexis
     
  3. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,846
    It looks like it could be a null Locale. There is a bug we are working on where the Initialization operation was not automatically called by LocalizedString and LocalizedAsset. You should not have to call the Initialization in this example however for the moment this should fix your problem

    Try doing what Alexis-Dev mentioned or alternatively use a coroutine.

    e.g
    Code (csharp):
    1.  
    2. private IEnumerator OnEnable()
    3. {
    4.    yield return LocalizationSettings.InitializationOperation;
    5.    var t1 = TEST.GetLocalizedString();
    6.    yield return t1;
    7.    if (t1.IsDone)
    8.    {
    9.        Debug.Log(t1.Result);
    10.    }
    11. }
     
    Estema_TheWoz likes this.
  4. Alexis-Dev

    Alexis-Dev

    Joined:
    Apr 16, 2019
    Posts:
    121
    Oh, I thought the Initialization operation call was a normal step to use LocalizedString...

    PS : Coroutines are ugly :p

    Best,
    Alexis
     
    karl_jones likes this.
  5. DatThax

    DatThax

    Joined:
    Jun 18, 2019
    Posts:
    10
    Thank you @Alexis-Dev and @karl_jones for the support. I ended up going with a Coroutine and now it works!
     
    karl_jones likes this.
  6. LezCT

    LezCT

    Joined:
    Nov 15, 2014
    Posts:
    55
    hi, I'm trying to use this to get the actual localized string to later use on a dialogue system effects like the typewriter effects. but I'm currently having this error:

    Can not preload when `LocalizationSettings.SelectedLocale` is null.
    UnityEngine.Localization.Settings.LocalizationSettings:get_InitializationOperation()


    if this is the same bug you mention, I understand.
    are there other ways to get the actual string, though?

    i'm using
    unity 2019.4.14f1
    localization package preview - 0.9.0
    addressables 1.16.6
     
  7. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,846
    This bug is fixed in 0.9.
    If the selected locale is null it could be because your locale selectors are not able to pick a starting language. Look at the settings and make sure the specific selector is set to one of your supported locales. Are you doing this in playmode?
     
  8. LezCT

    LezCT

    Joined:
    Nov 15, 2014
    Posts:
    55
    Ah, you're right! I wasn't doing this on playmode.
    how can I set the locale in edit mode?
     
  9. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,846
    We don't have editor mode support at the moment (0.9.0) but it will be coming in a future release.
     
    LezCT likes this.
  10. SamTyurenkov

    SamTyurenkov

    Joined:
    May 12, 2018
    Posts:
    84
    Localization package 0.9.0 used to work for me in recent Android release builds, but after I did something with addressables (clean build I think) I started getting these errors when calling Localized strings from scripts.

    Im not sure what I did exactly to break it, it was working without above mentioned
    yield return LocalizationSettings.InitializationOperation;


    But now I get these:
    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. UnityEngine.Localization.SmartFormat.Core.Parsing.Parser.ParseFormat (System.String format, System.String[] formatterExtensionNames) (at Library/PackageCache/com.unity.localization@0.9.0-preview/Runtime/Smart Format/Core/Parsing/Parser.cs:221)
    3. UnityEngine.Localization.SmartFormat.SmartFormatter.FormatWithCache (UnityEngine.Localization.SmartFormat.Core.Formatting.FormatCache& cache, System.String format, System.Object[] args) (at Library/PackageCache/com.unity.localization@0.9.0-preview/Runtime/Smart Format/SmartFormatter.cs:190)
    4. UnityEngine.Localization.Tables.StringTableEntry.GetLocalizedString (System.Object[] args) (at Library/PackageCache/com.unity.localization@0.9.0-preview/Runtime/Tables/StringTable.cs:94)
    5. UnityEngine.Localization.Settings.LocalizedStringDatabase.GetLocalizedStringProcessTableEntry (UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle`1[TObject] entryOp, UnityEngine.Localization.Tables.TableEntryReference tableEntryReference, UnityEngine.Localization.Locale locale, System.Object[] arguments) (at Library/PackageCache/com.unity.localization@0.9.0-preview/Runtime/Settings/Database/LocalizedStringDatabase.cs:197)
    6. UnityEngine.Localization.Settings.LocalizedStringDatabase.GetLocalizedStringAsyncInternal (UnityEngine.Localization.Tables.TableReference tableReference, UnityEngine.Localization.Tables.TableEntryReference tableEntryReference, UnityEngine.Localization.Locale locale, System.Object[] arguments) (at Library/PackageCache/com.unity.localization@0.9.0-preview/Runtime/Settings/Database/LocalizedStringDatabase.cs:175)
    7. UnityEngine.Localization.Settings.LocalizedStringDatabase.GetLocalizedStringAsync (UnityEngine.Localization.Tables.TableReference tableReference, UnityEngine.Localization.Tables.TableEntryReference tableEntryReference, UnityEngine.Localization.Locale locale, System.Object[] arguments) (at Library/PackageCache/com.unity.localization@0.9.0-preview/Runtime/Settings/Database/LocalizedStringDatabase.cs:166)
    8. UnityEngine.Localization.Settings.LocalizedStringDatabase.GetLocalizedStringAsync (UnityEngine.Localization.Tables.TableReference tableReference, UnityEngine.Localization.Tables.TableEntryReference tableEntryReference, System.Object[] arguments) (at Library/PackageCache/com.unity.localization@0.9.0-preview/Runtime/Settings/Database/LocalizedStringDatabase.cs:144)
    9. UnityEngine.Localization.Settings.LocalizedStringDatabase.GetLocalizedStringAsync (UnityEngine.Localization.Tables.TableReference tableReference, UnityEngine.Localization.Tables.TableEntryReference tableEntryReference) (at Library/PackageCache/com.unity.localization@0.9.0-preview/Runtime/Settings/Database/LocalizedStringDatabase.cs:109)
    10. CompleteProject.TankUnlocker.Awake () (at Assets/_GAME/Scripts/Managers/TankUnlocker.cs:54)
    Unity doesnt let me use OnEnable as couroutine, but I added that script into a persistent singleton, though it didnt help:
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.Localization.Settings;
    4.  
    5. public class InitLocalization : MonoBehaviour
    6. {
    7.  
    8.     private void Awake() {
    9.         StartCoroutine(LocStart());
    10.     }
    11.     private IEnumerator LocStart()
    12.     {
    13.         yield return LocalizationSettings.InitializationOperation;
    14.     }
    15. }
    Is it the same case as above or am I missing something?
     
  11. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,846
    That doesn't look like an initialization issue. Looks like something has gone wrong in the smart strings. Maybe an empty source or formatter. Have a look in the Localization settings under string database smart format. It may be worth deleting the settings asset and recreating it.
     
    SamTyurenkov likes this.
  12. SamTyurenkov

    SamTyurenkov

    Joined:
    May 12, 2018
    Posts:
    84
    Thanks, it solved the issue. I noticed that Formatters array was full of Empty elements before I recreated it.
     
    karl_jones likes this.
  13. hojjat-reyhane

    hojjat-reyhane

    Joined:
    Feb 4, 2014
    Posts:
    49
    Hi @karl_jones
    I can't use localization in Unity 2019.4.8f1. Checked with version 1.0.0 and 0.11.1.
    It gives me this error when playing the scene:

    NullReferenceException: Object reference not set to an instance of an object
    UnityEngine.Localization.Settings.LocalesProvider.ResetState () (at Library/PackageCache/com.unity.localization@1.0.0-pre.10/Runtime/Settings/LocalesProvider.cs:139)
    UnityEngine.Localization.Settings.LocalizationSettings.ResetState () (at Library/PackageCache/com.unity.localization@1.0.0-pre.10/Runtime/Settings/LocalizationSettings.cs:540)
    UnityEngine.Localization.Settings.LocalizationSettings.OnDisable () (at Library/PackageCache/com.unity.localization@1.0.0-pre.10/Runtime/Settings/LocalizationSettings.cs:215)

    I just add 2 locales and set specific locale to English, then played the scene.
     
  14. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,846
    1.0.0 should be out today/tomorrow. Can you try with that and if it still happens file a bug report.
     
  15. Widijou

    Widijou

    Joined:
    Sep 29, 2021
    Posts:
    3
    Hi there!

    I'm trying to use the Unity's Localization Asset, but it gives me errors all the time. I'm using Unity 2019.4.2f1, and I had this errors with Localization 1.0.0 to 1.0.3.

    I know very few about programming, so I can't figure how to fix this. May be this gets fixed with a new Unity version, I haven't tried that.

    These errors appear when creating a Localization Settings. It gives me this error when playing the scene:
    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. UnityEngine.Localization.Settings.LocalizedDatabase`2[TTable,TEntry].OnLocaleChanged (UnityEngine.Localization.Locale locale) (at Library/PackageCache/com.unity.localization@1.0.3/Runtime/Settings/Database/LocalizedDatabase.cs:512)
    3. UnityEngine.Localization.Settings.LocalizedDatabase`2[TTable,TEntry].ResetState () (at Library/PackageCache/com.unity.localization@1.0.3/Runtime/Settings/Database/LocalizedDatabase.cs:542)
    4. UnityEngine.Localization.Settings.LocalizationSettings.ResetState () (at Library/PackageCache/com.unity.localization@1.0.3/Runtime/Settings/LocalizationSettings.cs:571)
    5. UnityEngine.Localization.Settings.LocalizationSettings.OnDisable () (at Library/PackageCache/com.unity.localization@1.0.3/Runtime/Settings/LocalizationSettings.cs:242)
    And this when exiting:
    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. UnityEngine.Localization.Settings.LocalizedDatabase`2[TTable,TEntry].OnLocaleChanged (UnityEngine.Localization.Locale locale) (at Library/PackageCache/com.unity.localization@1.0.3/Runtime/Settings/Database/LocalizedDatabase.cs:512)
    3. UnityEngine.Localization.Settings.LocalizedDatabase`2[TTable,TEntry].ResetState () (at Library/PackageCache/com.unity.localization@1.0.3/Runtime/Settings/Database/LocalizedDatabase.cs:542)
    4. UnityEngine.Localization.Settings.LocalizationSettings.ResetState () (at Library/PackageCache/com.unity.localization@1.0.3/Runtime/Settings/LocalizationSettings.cs:571)
    5. UnityEngine.Localization.Settings.LocalizationSettings.EditorApplication_playModeStateChanged (UnityEditor.PlayModeStateChange obj) (at Library/PackageCache/com.unity.localization@1.0.3/Runtime/Settings/LocalizationSettings.cs:236)
    6. UnityEditor.EditorApplication.Internal_PlayModeStateChanged (UnityEditor.PlayModeStateChange state) (at <2cbbf8f8c622459ca9fa64194b7b6e6e>:0)
    7. UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)
    I have tried to reinstall the package, to delete the Localization settings, and I have searched on the forum following other solutions, without success. Any help?

    Thank you!
     
  16. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,846
    Can you share the project?
     
  17. denis_bogdanov

    denis_bogdanov

    Joined:
    Apr 20, 2015
    Posts:
    135

    Hello


    I had this problem a long time ago.
    It appears if the project settings window is "attached" as in the attached screenshots. if the window is detached, the problem disappears.


    How I decided to:
    1. Delete Localization Settings.
    2. Detach the project settings window
    3. Recreate the localization settings
     

    Attached Files:

    Last edited: Oct 5, 2021
  18. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,846
    Could you please file a bug report?
     
  19. denis_bogdanov

    denis_bogdanov

    Joined:
    Apr 20, 2015
    Posts:
    135
    Closer to the weekend or weekends, I will test if I can repeat the error, and if so, I will send.

    I have encountered a similar problem with the mobile notification package, it seems to be a problem with unity editor windows, not by localization package
     
    karl_jones likes this.
  20. Widijou

    Widijou

    Joined:
    Sep 29, 2021
    Posts:
    3
    In my case, the problem appears nevermind the project settings window is attached or not. I have tried detaching other windows, but it persists. Thank you anyway!

    The following is the link to the project in Google Drive. It has some placeholder assets, so it weights more than it should.

    https://drive.google.com/file/d/1LhowwhblrN-485sez7Ulx0criRzz-3Gt/view?usp=sharing

    Thank you!
     
  21. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,846
    Could you use the bug reporter please? So QA can process it. https://unity3d.com/unity/qa/bug-reporting
     
  22. Widijou

    Widijou

    Joined:
    Sep 29, 2021
    Posts:
    3
    Okay, I'll do it.
     
    karl_jones likes this.
  23. denis_bogdanov

    denis_bogdanov

    Joined:
    Apr 20, 2015
    Posts:
    135
    This error could not be reproduced, but I found another one
    (Case 1371884)
     

    Attached Files:

    • bug.rar
      File size:
      1.1 MB
      Views:
      226
    karl_jones likes this.
  24. Estema_TheWoz

    Estema_TheWoz

    Joined:
    Aug 5, 2021
    Posts:
    7
    Thanks for the reply, it works well in my case.