Fixing the missing assembly reference error in Unity projects
When working with code that uses multiple assemblies or use other third party packages you might run into something like the following:
The type or namespace name ‘ClassName’ could not be found (are you missing a using directive or an assembly reference?)
You try using your IDE’s intellisense to come up with a using import but all you get are options on how to generate the class file, even though the imported scripts are right there. The scripts were imported alright so what caused the issue?
To experiment with assembly references on a clean small project see the sample on Github.
Working with assemblies in Unity
Assemblies are collections of types and resources that are built to work together and form a logical unit of functionality. By default, Unity compiles your scripts into a predefined assembly, Assembly-CSharp.dll, but for larger projects this monolithic approach could slow down the compilation time for iterative code changes. By defining assemblies, you can effectively establish modularity and reusability.
Read more on assemblies in the Unity manual or the .NET docs.
As such, when importing another package, you’re using their assembly and it has to be referenced in your code’s assembly for you to actually use it.
Referencing another assembly in your code
The example project I’m giving you is tiny for the sake of simplicity but it’ll do good enough to explain the concept.
Suppose we have a GameplayManager
that controls the overall state of the game, including enabling and disabling controls. Then we have our player’s movement control PlayerController
and PlayerWeapons
. To give an example of using third party Unity packages I’m also referencing an imported Asset Store project, in this case my Aim Assist package.
You’re probably familiar with .cs
files already. The assembly definition files are the .asmdef
files. Note how they are placed in the directories of the project. Code files that are in the same level, or under subdirectories of the assembly definition file belong to the said assembly definition.
Gameplay
, Player
and the third party code are separate assemblies and they do not know of each other automatically.
Both PlayerController
and PlayerWeapons
depend on GameplayManager
to tell it if controls are enabled. PlayerWeapons
also uses the third party package. However, when you try to compile you get the missing reference error even if you add the necessary using
statements. It is also alarming that your IDE’s intellisense isn’t prompting a suggestion to a class.
Solution to the problem
The solution is actually surprisingly simple, provided you know what assemblies are and how the fit into a project.
Inside the Unity Editor, find the .asmdef
file for your Player:
Open up your Inspector and see the options:
The marked field Assembly Definition References contains no references to any other assembly. As a result all the other code files inside those assemblies are invisible for this assembly. Using GUID
s to identify assemblies is the way recommended by Unity.
Hit the little +
icon twice to create two reference slots. We then select the available assembly definition files by clicking the little knob to the right of the reference slot:
Then at the end, hit Apply. This included the references to your .asmdef
file:
Now back inside your IDE bring up your import suggestions, and you should see the using
statement pop up:
Now you should have a nice and clean Unity Console window that shows no assembly reference errors and you’re good to go.
Thank you for reading my article, I hope it helped you fix your project’s assembly reference issues. If you’re interested in what I’m up to, please check out my socials or asset store. Happy coding!