0.51 InvalidOperationException: Cannot find Unity.NetCode metadata reference for assembly

error Netcode: System.InvalidOperationException: Cannot find Unity.NetCode metadata reference for assembly BovineLabs.Saving
   at Unity.NetCode.Generators.TemplateParser.ParseTemplatesFromMetadata(GeneratorExecutionContext context, IList`1 templates)
   at Unity.NetCode.Generators.TemplateParser.ParseTemplates(GeneratorExecutionContext context)
   at Unity.NetCode.Generators.NetCodeSourceGenerator.Generate(GeneratorExecutionContext executionContext, Candidates candidates, IDiagnosticReporter diagnostic)
   at Unity.NetCode.Generators.NetCodeSourceGenerator.Execute(GeneratorExecutionContext executionContext)

I’m having an immediate issue after updating to 0.51 where if I have an assembly that does not use netcode is referenced by an assembly that does use netcode I just get the above locking error. It’s fixable by making the assembly reference Netcode.

e.g.
GameAssembly

  • References Netcode
  • References Saving

SavingAssembly

  • References nothing

Above error
Adding Netcode to SavingAssembly fixes error.
This happens in a brand new project just adding in in 2 existing libraries.

-edit- actually I think it might be reversed

Core references Netcode
Saving references Core

Even if saving does not use any Netcode it needs a reference on Netcode otherwise errors

What editor version are you using?
We fixed that problem recently and I’m honestly quite surprised you have that issue. Since there are tons of assemblies that does not used NetCode that are processed without problems.

Latest 2021.3.4f1 (latest)

Packages

   "com.unity.burst": "1.7.2",
   "com.unity.entities": "0.51.0-preview.32",
   "com.unity.netcode": "0.51.0-preview.32",
   "com.unity.physics": "0.51.0-preview.32",
   "com.unity.rendering.hybrid": "0.51.0-preview.32",

I had it on 4-5 separate assemblies (of like 50) when upgrading - including 2 test assemblies.

mmm… I tried doing the same in a very small project with two assemblies like you mentioned.
But I have zero error. The setup looks like:

AssemblyWithNetCode
ref Entities
ref NetCode
ref Burst
ref Collection
ref Mathematics
ref AssemblyWithoutNetCode

AssemblyWithoutNetCode:
ref Entities
ref Burst
ref Collection
ref Mathematics

The assemblies only contains one file, with one component each.
The assembly that has netcode references also replicate that component (he has ghost fields)

Also, given the error you get I suspect the problem is elsewhere. In particular, the compilation pipeline is passing/running (erroneously) the NetCodeSourceGenerator to assemblies that does not reference netcode. That is wrong, it is not the expected behaviour. This is why that method trigger an exception.
We can fix in netcode package by adding some extra checks and skip the problem altogether. But I would like to be able to repro the problem myself first.
I might suspect that is an issue due to the upgrade / update. If I start from scratch a project I might not be able to see any issue there.
Can you please check that if you now, after you updated everything, remove all the netcode reference, delete the library folder and re-import the project, everything works?

Actually I got the error in a a brand new empty project copying in libraries in. When upgrading my project I was just getting a generic errors that didn’t help at all and wouldn’t compile (even though I deleted entire library before upgrading).

Luckily my project is extremely modular so instead I created a fresh project and slowly brought libraries into it until I got errors. Once I fixed these up and copied back changes to original project it compiled fine.

When I get off from work I’ll try remove the references I added and see if it still fails.

I can probably rollback to my pre-upgrade state, test if I can repo the same thing in an empty project and if so just send you a library that fails.

Thanks that will help understand what it is going on.
But in any case, I have already an hot-fix for the problem that we can land to you asap.

Hi, have the same issue. can you also point me to your hotfix @CMarastoni ? thanks

Can you guys please report this issue using the UnityEditor bug report ? So we can keep track of it.
I was unable so far to repro the problem, but I’m probably doing something different you are. Would be great if you can provide a project (hopeufully a test project) where the problem occurs.

In order to give you the hot-fix I need to re-compile the netcode-generator and push some dlls in the forum.
That is usually not what I would like to do, I would prefer to release a proper package fix instead (seem more legit).
Let me see what the ETA for a package update would first.

Oh sorry I forgot about this as once solved it was no longer an issue.
I’ll try find some time to see if my expected repo works.

I’m also getting the error with that kind of setup

AssemblyA references NetCode
AssemblyB references AssemblyA
→ this gives me “Cannot find Unity.NetCode metadata reference for assembly AssemblyB”

But as soon as AssemblyB also explictly references NetCode, the error goes away

Hi Phil,
Can you please share the project (or a similar setup) where the problem occur?
I tried doing the same in a new project or even in older ones but I’m not able to repro it, even though I have a similar setup.
Question for you: when you got this problem, there was some script error or api update or your were in safe-mode?

So, since there are already 3/4 persons that are getting this issue I decided to publish an hot-fix here (even though is not the best but lets try).
Attached you’d find the new compiled version of the source generator that should fix that issue.

Please (if you didn’t already) move the com.unity.netcode from the Library/PackageCache into your package folder (or another directory and then set the manifest accordingly) and unzip the dlls into the com.unity.netcode/Runtime/Gen folder (where they currently are)

Could you also please let me know if that fix your problems?

8231985–1075725–HotFix-Generators.zip (34.5 KB)

After applying the hotfix, I’m getting this error:

First line says “Cannot load template …/GhostComponentSerializer.cs” and “Package com.unity.netcode not found”, but GhostComponentSerializer.cs does exist at the specified path, and my custom netcode package is detected by the package manager

EDIT: actually, I get this same error even when I just copy the netcode folder into my Packages folder and remove netcode from my package manifest, without applying the hotfix. I might be doing something wrong?

I also haven’t been able to repro in a new project, even when starting out in 2020.3 and then upgrating to 2021.3

ohh, that is not what I was expecting to see, I apologies, I tried my self the same fix but I didn’t get that error. I will by moving netcode in the package folder and fix the problem.

Not sure if this is the issue you are seeing, but there is one big pitfall with doing this - when you copy the folder into the package folder you must rename it so the path is Packages/com.unity.netcode/ - if you just move it and keep the @0.51.0-preview in the folder path it will not work.

@CMarastoni

I see, I’ve tried it again and now I can confirm the fix works. I was able to remove the NetCode reference from some of my assemblies and the errors don’t appear anymore. Thanks!

I won’t be able to use the hotfix for my project specifically, because it’s an asset store project. But I’d be interested in knowing if there are plans to release an official package update containing the fix in the near future?

Yes, definitely.

1 Like

Hi @CMarastoni . I’m getting these messages with or without an assembly definition while following Networked Cube | Netcode for Entities | 1.0.17 as soon as I create the “Game.cs” script under the “Establishing a connection” section. The above steps don’t seem to do anything (which isn’t a surprise given there’s no com.unity.netcode/Runtime/Gen folder in the latest version).

Anyway, any help would be appreciated.

Is the package embedded in the Package folder? In the latter, did you remove the " @ @ suffix (the package name should be com.unity.netcode)

Since there is no …/Runtime/Gen folder, I set it up this way:

On a hunch, I also tried deleting the existing “NetCodeSourceGenerator.dll” and renaming the Hotfix file “Unity.NetCode.SourceGenerators.dll” to match, but that didn’t make a difference.

In both cases, Unity boots into safe mode and gives me variations on:
Packages\com.unity.netcode\Runtime\Physics\PhysicsVelocityVariant.cs(32,34): error CS0433: The type ‘GhostPrefabType’ exists in both ‘GeneratorShared, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null’ and ‘Unity.NetCode, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null’