I am trying to refactor some code, turn it into a package, put it in a Git repository, and import it into another project as a Git package import. I have managed to do so and import it without a problem (it is listed in the Packages in the Editor. I can also see all the assets in it). However, when I create a new script in the project that imported the package, and I try to edit it with the text editor, none of the scripts in the package are found. I can open the script in the package by double clicking it directly from the editor, but I don’t seem to have any reference to it. Even when I try to drag and drop one of these scripts to a GameObject in the hierarchy, I get the error “Can’t add script component because the script class cannot be found…”.
The classes in the package are properly named (same as file name) and inherit MonoBehaviour. I am using Unity 2020.3.15f2, and the scripts are edited using VS 2019 Community.
Do I have to do something more so I can use these scripts? The scripts worked perfectly fine before I refactored them as a package and imported it. Thanks in advanced.
If you enable «Auto Referenced» on the assembly definition, your scripts in the Assets folder should be able to pick up the scripts from your packages directly. If you use assembly definitions in Assets, you’ll have to reference the package’s assembly definition explicitly.
TL;DR: This works but it breaks when the scripts are inside a namespace, the ugly, makeshift solution is to not use namespaces on packages at all.
I’d like to add a couple notes to this as it does only partially cover the issue, adding the asmdef will however not solve it for everyone. I have been tracking this issue down for the last couple hours. Started with some “complex” code I wanted to remove from the Assets folder, which works perfectly fine in the Assets folder, has no path references that could be breaking or anything. it just wouldn’t work from the packages folder.
I think it’s worth mentioning that we always use amsdefs anyway to speed up compilation times, and always keep implementation examples and code that works only on the editor side isolated with their own asmdefs.
Also I’d like to add that I always upload my git packages from Unity 2018.2 as the Asset DB2 introduced in 2018.3 makes it so that prefabs created from that version onwards can’t be imported in 2018.2 and lower. This however causes the “Auto Reference” option disappear from the inspector, but it will be checked when importing the package (At least in 2021 and 2022, can’t confirm other versions.)
So back on track. Nothing I tried worked until I made a sample package with the default script that get’s created when you click on new C# script. The only difference (apart from functionality) was that I was wrapping it all in namespaces. I removed the namespace and it automagically works. I’m assuming this is a bug right?
But there has to be a compounding factor. There are tons of packages in use, from Unity and third-party developers, that use namespaces and work perfectly fine. I’ve been using and creating packages for a long time and never had issues with scripts being in namespaces.
Maybe there’s something with using 2018.2? I’m usually on newer Unity versions, so I can’t speak for your specific workflow.