Newtonsoft error using PM

Hi,

Not sure if this is the correct place to be posting this, but I was unsure where else it may belong.

I’m getting this error when I try to use the Newtonsoft.Json namespace:

error CS0246: The type or namespace name ‘Newtonsoft’ could not be found (are you missing a using directive or an assembly reference?

I have used Newtonsoft before, and believe I’m doing things correctly, but to no avail. I have thoroughly googled this error, and tried suggestions but the error still persists. I have tried installing older versions, I’ve triple checked my references. I’m baffled at the moment. Any help would be appreciated!

Hi @ItzChris92 ,

I presume you’re talking about the com.unity.nuget.newtonsoft-json package. Please note that this package is currently intended for internal use. For now it can only be referenced through the use of an Assembly Definition file by checking “Override References” on it, and adding the DLL as an explicit assembly reference. See the Backwards compatibility and implicit dependencies section of Unity - Manual: Assembly definitions for more info.

Thanks. I also tried downloading newtonsoft and placing it in my plugins folder, but the same error appears

Oh, this might be a problem with the same assembly being present in the project multiple times - perhaps the Compilation Pipeline gets confused about which one to use, and only considers the Importer Settings for one of them (in the package). Did you try with the DLL, or the source code? You could try with the source code in your project (with or without an .asmdef to make it a separate assembly) and see if that works.

I put the source code in the plugins folder. Is this correct or should it just be the correct dll file in there, and the reference added on VS? Then I imagine the source code would go in the packages folder for my project? I’ve not had much experience using dll files, but I did not seem to have any problems the last time I used newtonsoft. Apologies for the noob questions

Did you put that code/DLL in an Editor folder under Plugins? Otherwise your Editor code will not be able to access it.

1 Like

No i didn’t. I thought that was only necessary for editor extensions.

For the time being I have just installed the json. Net from the asset store. I’ll come back and try again another time if I find there’s a need for the newer versions of newtonsoft. Thanks for the replies

I’m sorry, You’re right, I got confused for a moment. Indeed it doesn’t have to be under an Editor folder since it’s not accessing Editor APIs.

So, I can’t really say what’s wrong in your case. Take a look at the Editor.log file, there may be hints. Otherwise I recommend opening a Bug report using Help > Report a Bug to get further personalized help.

I’m new to Unity and pretty confused. I followed these instructions and have “JSON.Net 12.02 for Unity” in the package manager. I don’t see a DLL anywhere… Can you give me a clue as to how I might get it?

1 Like

Hi @k7n4n5t3w4rt ,

The Package Manager doesn’t add any DLL to your Assets directly. Instead the packages are loaded in a separate virtual folder hierarchy that starts with Packages/.... If the above instructions result in the JSON.Net 12.02 for Unity package being listed in the Package Manager, you can Install it into your project and assuming the package is correct, the DLL will be available for your code to use.

(Note: the instructions and site you linked are not official Unity documentation, and neither is the package - you may want to contact the author of that Git repository if there are any additional problems.)

1 Like

My issue was that the test script I put in /Assets/Tests/EditorTests/ couldn’t use the namespace of the package. Scripts in the /Assets/scripts/ worked, no problem. Perhaps something I could fix with assembly definition files.

Solved: Apparently, in tests, it’s necessary to specify “Assembly References” specifically for packages installed with the Package Manager:

https://github.com/jilleJr/Newtonsoft.Json-for-Unity/issues/39

1 Like

I have encountered a similar problem.

I am using Ros# he requires Newtonsoft.Json.dll assembly in another assembly called Newtonsoft.Bson.dll.
Now the Unity has its own implementation of Nuget.Newtonsoft.Json.dll needed for DOTS and I get errors.
If I have the Ros# Newtonsoft.Json.dll in the Plugins folder I get an error that there cannot be two identical assemblies, so it is clashing with the Unity’s one, but if I delete Ros# assembly then I get an error that the Newtonsoft.Bson.dll cannot find Newtonsoft.Json.dll, so it doesn’t recognize Unity’s implementation of Newtonsoft.Json.dll.

Is there a way to fix this issue?

And like almost all Unity threads, no good, and useful answer from Unity team. I’m so glad I didn’t buy Unity auctions. I’ve already paid $350 x 2 for 2 years it’s far too much for the help I got.
In the previous versions it didn’t work, I found a workaround (like you have to do with all the Unity projects… in fact a Unity game is a 99% compilation of workarounds). Then a few versions later, Unity came bundled with this Newtonsoft (and like Unity do, no official documentation) so my “hack” didn’t work anymore, I had to remove it, and now, for Unity 2021.2.13, it doesn’t work AGAIN and I have to lose AGAIN hours and hours to make it work. I really tried my best to not try Unreal Engine, but seriously, this lack of professionalism is definitely getting on my nerves.

1 Like