- Home /
Cannot resolve unity container
I am having a ton of trouble trying to use UnityContainer. Unity will not compile my scripts as it keeps giving me this message :
'The type or namespace name Unity could not be found. Are you missing an assembly reference?'
'The type or namespace name UnityContainer could not be found. Are you missing an assembly reference?'
I have been digging around on the web for ages, and have found some people with similar, but not exactly the same problem. So far I have tried :
Uninstalling / Reinstalling Unity (2017.3.0f3)
Uninstalling / Reinstalling Visual Studio ( 2017 )
Changed unity player settings to use Experimental .NET 4.6 equivilant
I have used NuGet to install the Unity Container project, and everything resolves and builds fine in Visual Studio, but Unity keeps giving the missing Namespace error.
I am completely stuck, and at a bit of a loss because I need to use the features UnityContainer provides in order to progress with my game. I have considered using an older version of Unity, but I really want to use the new tilemaps feature. I would be incredibly grateful for any assistance.
Answer by Alanisaac · Dec 23, 2017 at 08:48 PM
AFAIK, Unity and NuGet don't play nicely by default. Have you tried the suggestions in this article? You might be hitting the issue where Unity essentially wipes out your packages. I'll note that the "old version of mono" isn't really an issue anymore if you're targeting the 4.6 equivalent. From that article:
The difficulties of using NuGet with Unity boil down to the following issues:
Unity is built on an old version of Mono. So we are limited in what we can use from NuGet.
Unity sets our Target Framework by default to something that will prevent most NuGet packages from being installed.
Unity regenerates its Visual Studio solution, which wipes out installed packages.
Unity only recognizes DLLs placed under the Assets folder, by default NuGet won't install packages where we want them.
Will have a read now, thanks for the response. Will let you know how I get on
Ok, so I think I have fixed it. I skimmed through the article, and the symptoms made sense. Firstly I thought, Perhaps if I add the unity reference directly to the project ins$$anonymous$$d of relying on package manager. But then realized you cant do that. So I added a Pluggins folder in my assets directory, and dragged the appropriate assemblies in from the package, now it is compiling! Do you think this is an appropriate course of action?
That definitely works. If you're happy with that solution, I'd say stick with that. If you'd like to take it a step further and NuGet working, the relevant section of that article is under "Nuget config file". Reproduced the snippet from that article below. Note that you may have to restore packages after modifying the configuration.
To install NuGet packages into Unity and have them survive Unity's regeneration of the solution, you must create a custom NuGet configuration file that instructs NuGet to store it's packages under Unity's Assets folder ins$$anonymous$$d of under a packages folder next to the solution (which isn't in the Assets folder). Our DLLs must live under the Assets folder so that Unity can find them and including them in the solution when it is regenerated.
Create a file called nuget.config next to the solution for your project (the sln file).
Note the packages.config file. NuGet generates this file (it won't exist until you install your first package), it contains a list of packages you have installed in the project.
The only setting we need in nuget.config is repositoryPath. We use this to tell NuGet to install packages under the Assets directory. Your nuget.config should look like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="repositoryPath" value=".\Assets\packages" />
</config>
</configuration>
After creating the NuGet config file you should restart Visual Studio to make sure the config changes take effect.
Ugg, so unity builds it, but now it does not build in VS, I am going to follow that article and see if that fixes it
I have followed the guide, and I am progressing. I am just getting weird issues where unity is messing with the sln. If i create a new project and add the nuget config file, I get no compiler errors in VS or Unity. Once I start adding scripts and doing stuff to them, Unity is doing something to the solution as I keep getting the "Solution modified outside of the environment" message, then things go wrong. The nuget config file gets deleted, and I start getting build errors in VS
Thanks a million. I have managed to get it working now, really appreciate your help :)
Answer by ryandotdee · Dec 23, 2017 at 11:51 PM
I would love to say it was easy, but, it was not lol. But I think I am there now.
Change player settings to target .NET 4.6
Create nuget.config file
Install UnityContainer via nuget
Unity will complain about duplicate references, so go into assets/packages/Unity .5.5.0/lib/ and delete every folder except net45
I was compounding my problem as I switched back to VS 2015 while I was trying to work this out which did not have the right .NET targeting pack installed. IF you use latest VS, your csproj file will retain the correct target framework which keeps it all working