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

Resolved The type or namespace name 'Localization' does not exist in the namespace 'UnityEngine'

Discussion in 'Localization Tools' started by bottledgalaxy, Jul 16, 2020.

  1. bottledgalaxy

    bottledgalaxy

    Joined:
    Jul 19, 2019
    Posts:
    83
    I successfully made the package work for changing texts in the Editor for GameObjects.

    Now I wanted to change text that I have in scripts, and when putting

    Using UnityEngine.Localization;
    at the top, I'm getting the error

    The type or namespace name 'Localization' does not exist in the namespace 'UnityEngine' (are you missing an assembly reference?)


    Am I missing a step somewhere?

    Thanks!
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    Are you using an Assembly Definition file (asmdef)? You need to add the Localization asmdef to it as a dependency.
     
  3. bottledgalaxy

    bottledgalaxy

    Joined:
    Jul 19, 2019
    Posts:
    83
    I don't even know what that is... so I'm assuming no? Where would I check that?
     
  4. bottledgalaxy

    bottledgalaxy

    Joined:
    Jul 19, 2019
    Posts:
    83
    Ha!! Looks like I was! Thank you for that!
     
    karl_jones likes this.
  5. Claite

    Claite

    Joined:
    Sep 26, 2015
    Posts:
    8
    @karl_jones I'm running into this same issue but I don't have an asmdef file. I'm on Unity 2020.3.15f1, Localization package 1.0.0-pre.10.

    I can create the localization components on the UI and add them to my TextMeshPro gameobjects
    Created the strings table and referenced the string I want
    Created a Global variables group with a global variable that's picked up by the component and updates appropriately

    But I can't access the global variables group through code since it complains that Localization isn't a namespace within Unity Engine. Is there another place that I need to be referencing the package? This is the first time I've run into this kind of issue even with using the Analytics package and other non-standard ones.
     
  6. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    What's the full error message?
    What's the code look like?
     
  7. Claite

    Claite

    Joined:
    Sep 26, 2015
    Posts:
    8
    @karl_jones Thanks for the fast response.

    Full error message:
    The type or namespace name 'Localization' does not exist in the namespace 'UnityEngine' (are you missing an assembly reference?) [Assembly-CSharp]csharp(CS0234)

    Code is just trying to access LocalizationSettings object:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Analytics;
    5. using UnityEngine.UI;
    6. using UnityEngine.Localization;
    7. public static class GameData {
    8.     public class Settings {
    9.         private bool soundEnabled = true;
    10.         private bool musicEnabled = true;
    11.         private bool notificationEnabled = true;
    12.  
    13.         public Settings() {
    14.             var source = LocalizationSettings
    15.                 .StringDatabase
    16.                 .SmartFormatter
    17.                 .GetSourceExtension<GlobalVariablesSource>();
    18.         }
    19.     }
    20. }
     
    Last edited: Jul 29, 2021
    -chris likes this.
  8. Claite

    Claite

    Joined:
    Sep 26, 2015
    Posts:
    8
    To test, I tried loading a sample from the package manager into the project. The moment I did so the errors resolved themselves. Seems a bit strange the package was installed but somehow my own scripts couldn't reference the namespace but when a sample was loaded it could.

    Is there some hidden build file or similar inside of Unity for how it resolves imports? The package manager showed it installed...

    Either way, if anyone else runs into this try just importing a sample into the project to see if that resolves the reference issue.
     
  9. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    LocalizationSettings is in the namespace UnityEngine.Localization.Settings. I think you just needed to add an extra using at the top.
    Not sure why adding a sample fixed it.

    Although it does sound like something else is going on.
     
    adrianfrancisco likes this.
  10. ninjanosui

    ninjanosui

    Joined:
    Jul 12, 2013
    Posts:
    54
    Yes same solution for me. Adding a sample fixed it.
     
  11. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    Did you put your code inside the same folder as the sample?
     
  12. sirimanasa

    sirimanasa

    Joined:
    Aug 6, 2020
    Posts:
    1
    What sample is it? How do I add it?
    I am stuck with the same problem.
     
  13. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
  14. TortugaXel

    TortugaXel

    Joined:
    Oct 18, 2013
    Posts:
    4
    Hello, I'm running into the same issue. I can't access the UnityEngine.Localization namespace after installing the Localization package and I don't know how to reference the localization asmdef. Currently I had not run into this issue with other namespaces form other installed packages. The Unity.Localization.dll is in the project Library > ScriptAssemlies. How should I fix this?

    Thanks in advance
     
  15. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    You should be able to access it like any other package namespace. Can you show me what you are doing? Where are your scripts?
     
  16. TortugaXel

    TortugaXel

    Joined:
    Oct 18, 2013
    Posts:
    4
    Hello Karl! And thanks for reaching out. On the day I installed the Localization package I was trying to use the UnityEngine.Localization namespace but it could not be found, even though everything was installed correctly. I even tried to close and reopen my Unity project but that did not work either. Today after restarting my computer the issue was gone and now I can normally use the Localization namespace. Maybe turning the computer off and then back on may fix the issue for other people.

    Thanks again for reaching out.
     
    ilmario and karl_jones like this.
  17. noabody

    noabody

    Joined:
    Sep 26, 2018
    Posts:
    2
    I just observed an error similar to this in Daggerfall Unity. The included code was:
    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. //using UnityEditor;
    4. //using UnityEditor.Localization;
    5. using UnityEngine.Localization.Settings;
    6. using UnityEngine.Localization.Tables;
    With error message "cs0234 namespace localization does not exist in UnityEditor"

    I added a sample and that did resolve the issue. At which point I started removing and manipulating until I reached this statement "namespace UnityEditor.Localization{}". Simply adding that allowed the build to continue.
    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using UnityEditor;
    4. using UnityEditor.Localization;
    5. using UnityEngine.Localization.Settings;
    6. using UnityEngine.Localization.Tables;
    7. namespace UnityEditor.Localization{}
    Following that principle, this statement would probably resolve the poster's issue "namespace UnityEngine.Localization{}"
    "Resolve" is a pretty hard term, work around, or fake would be more appropriate.
     
  18. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    That seems very strange. Can you not just remove the line 'using UnityEditor.Localization'?
    If you don't need the using statement then remove it or you get errors like above.
     
    Last edited: Nov 24, 2022
  19. noabody

    noabody

    Joined:
    Sep 26, 2018
    Posts:
    2
    I had a question for you Karl. The only analogue I can think of is that "using UnityEditor.Localization;" would be similar to "#include <string>" in that it's some kind of include. Unlike a C++, however, it seems that a code reference is required. I could use "#include <string>" without ever calling a function like "std::string" and that wouldn't generate an error.

    In this case it seems as if using the include "using UnityEditor.Localization;" requires a function of the namespace to exist somewhere within code so that a corresponding "namespace UnityEditor.Localization{}" is required. In the examples, this wouldn't be the case. It seems like we just need the root when a function is built, something like "namespace UnityEditor.Localization.Work{}" where "Work" references "UnityEditory.Location" and thus becomes a sub-function.

    Just keep in mind that I am not a coder and don't really know how any of it works. I'm pretty good at sleuthing out bugs that prevent a program from compiling, but that's just the application of other people's solutions.

    Edit:
    I was seeing "namespace UnityEditor.Localization.Samples" and thought that meant "Samples" was user defined but I can see in the documents that that is, in-fact, a global namespace. It looks like code should normally be grouped within one of these defined namespaces? That should prevent the error. ASMDEFs aren't used in Daggerfall Unity, so maybe having an ASMDEF would change the behaviour slightly?

    My guess is that placing the code within a global namespace is what triggers the include to be "linked" or "loaded". Pure speculation, though.
     
    Last edited: Nov 24, 2022
  20. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    There is localization editor code under this namespace but it's not being included in your code, likely due to the asmdef differences, so it doesn't find the code and reports the missing namespace. If you did 'using some.namespace' it would also produce an error if the namespace did not exist anywhere. Removing the using statement is the correct approach.
     
  21. ltlejeune

    ltlejeune

    Joined:
    Aug 6, 2019
    Posts:
    24
    Hey all,

    I have the exact same issue. I'm using 2020.3.5.f1.
    Please, what are the exact steps I have to do so that Unity recognize this namespace ?
    (I have no asmef specifically defined, plain creation from the Unity Hub)

    Thanks, regards.
     
    Last edited: Dec 22, 2022
  22. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    Do you have
    Code (csharp):
    1. using UnityEngine.Localization;
    at the top?

    Do you have the localization package installed?
    Where is your script that is causing the issue?
     
    ltlejeune likes this.
  23. ltlejeune

    ltlejeune

    Joined:
    Aug 6, 2019
    Posts:
    24
    Code (csharp):
    1. using UnityEngine.Localization;
    -> I have it at the top, that is the reason why Unity/Class loading is complaining about this dependency.

    Do you have the localization package installed?
    -> I don't know where to check... since in the package Manager there are no traces of a package containing the "Localization/Locale" string (checked in Unity Registry only). I'd imagine it's bundled naturally whenever I created a URP 2D project ? Perhaps should I install it from a specific URL ?

    Where is your script that is causing the issue?
    -> My script is under Assets/scripts/...

    Thanks for the anwser :)
     
  24. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
  25. ltlejeune

    ltlejeune

    Joined:
    Aug 6, 2019
    Posts:
    24
    Thanks, I could install it... I had to downgrade my existing newtonsoft library to match the dependency of the package. (hope no regressions will be there...)

    However, now I have the following in the console, I presume due to the adressable dependency.
    Code (csharp):
    1. Library\PackageCache\com.unity.addressables@1.20.5\Runtime\AssetReference.cs(924,27): error CS0117: 'AssetDatabase' does not contain a definition for 'SaveAssetIfDirty'
     
  26. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    That API should exist. Try updating to the latest patch version of 2021.3.
     
  27. ltlejeune

    ltlejeune

    Joined:
    Aug 6, 2019
    Posts:
    24
    My Mistake I was on 2020.3.5.f1 NO 2021.3.5.f1.
     
  28. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    You will need to update to the latest 2020.3
     
  29. ltlejeune

    ltlejeune

    Joined:
    Aug 6, 2019
    Posts:
    24
    Taking the latest (Unity 2020.3.43) worked our for me. Thanks Karl !
    Now I've got things compiling, and the project run (no regression on the upgrade from 3.15 luckily observed).

    So now If I want to take the language from steam, do I still need to use the SteamWork APIs (c#) or should I use somehow a wrapper from this newly installed lib ? (I understand this lib is mainly for organizing localized specific assets, but I've got rooted to install it from another thread suggesting to do so when willing to get steam default language)
     
    Last edited: Dec 22, 2022
  30. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    ltlejeune likes this.
  31. ltlejeune

    ltlejeune

    Joined:
    Aug 6, 2019
    Posts:
    24
    yep was working on my way for that...
    Code (csharp):
    1.  
    2. [DisplayName("Steam Selector")]
    3.     [Serializable]
    4.     public class SteamStartupSelector : IStartupLocaleSelector {
    5.         public Locale GetStartupLocale(ILocalesProvider availableLocales) {
    6.             var lSteamLoc = SteamManager.instance.steamLocale;
    7.             Debug.Log(string.Format("steam locale:{0}", lSteamLoc));
    8.             return availableLocales.GetLocale(lSteamLoc);
    9.         }
    10.     }
    11.  
    Thanks Karl, I guess the rest I'll have to test from the build itself since the steam API do not work in the Editor... :)
    Bye.
     
    karl_jones likes this.
  32. ltlejeune

    ltlejeune

    Joined:
    Aug 6, 2019
    Posts:
    24
  33. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    It gets called during the localization initialization. You should not have to call it yourself.
     
  34. gimalfan

    gimalfan

    Joined:
    May 20, 2019
    Posts:
    3
    Hey all,
    Errors appear when building a project:

    Code (CSharp):
    1. Assets\Localizations\Local\DataTest.cs(1,19): error CS0234: The type or namespace name 'Localization' does not exist in the namespace 'UnityEditor' (are you missing an assembly reference?)
    Code (CSharp):
    1. Assets\Localizations\Local\DataTest.cs(8,30): error CS0246: The type or namespace name 'StringTableCollection' could not be found (are you missing a using directive or an assembly reference?)
    How can this problem be solved?

    The script is located in the Assets folder, I have no asmef specifically defined, tested on Unity 2021.3.11, 2021.3.19, 2022.2.2.
    Here is an example of the code where the error occurs:

    Code (CSharp):
    1. using UnityEditor.Localization;
    2. using UnityEngine;
    3. using UnityEngine.Localization.Components;
    4. using UnityEngine.Localization;
    5.  
    6. public class DataTest : MonoBehaviour
    7. {
    8.     [SerializeField] private StringTableCollection table;
    9.     [SerializeField] private LocalizeStringEvent local;
    10.  
    11.     private string key = "first";
    12.     void Start()
    13.     {
    14.         SetString(key);
    15.     }
    16.  
    17.     public void SetString(string key)
    18.     {
    19.         local.SetTable(table.name);
    20.  
    21.         local.SetEntry(key);
    22.     }
    23. }
     
    Last edited: Feb 23, 2023
  35. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
  36. gimalfan

    gimalfan

    Joined:
    May 20, 2019
    Posts:
    3
    Yes indeed, you are right.
    I would like to get a list with all the keys in a certain table.
    The StringTableCollection has a GetrowsEnumerator() method for working with keys in a table, but is there something similar for a LocalizedStringTable?
     
    Last edited: Feb 27, 2023
  37. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    Yes you can use the string tables SharedData.Entries property.
     
  38. gimalfan

    gimalfan

    Joined:
    May 20, 2019
    Posts:
    3
    Oh, thanks, it worked.