Failure Generating Network Code [SOLVED]

I have been working on switching over to UNET from the legacy networking system for a while and everything was going smoothly until yesterday the compiler stopped working giving me the following error:

UNetWeaver error: Exception :System.ArgumentException: Member ‘UnityEngine.Networking.NetBuffer UnityEngine.Networking.NetworkWriter::m_Buffer’ is declared in another module and needs to be imported
at Mono.Cecil.MetadataBuilder.LookupToken (IMetadataTokenProvider provider) [0x00000] in :0
at Mono.Cecil.Cil.CodeWriter.WriteOperand (Mono.Cecil.Cil.Instruction instruction) [0x00000] in :0
at Mono.Cecil.Cil.CodeWriter.WriteInstructions () [0x00000] in :0
at Mono.Cecil.Cil.CodeWriter.WriteResolvedMethodBody (Mono.Cecil.MethodDefinition method) [0x00000] in :0
at Mono.Cecil.Cil.CodeWriter.WriteMethodBody (Mono.Cecil.MethodDefinition method) [0x00000] in :0
at Mono.Cecil.MetadataBuilder.AddMethod (Mono.Cecil.MethodDefinition method) [0x00000] in :0
at Mono.Cecil.MetadataBuilder.AddMethods (Mono.Cecil.TypeDefinition type) [0x00000] in :0
at Mono.Cecil.MetadataBuilder.AddType (Mono.Cecil.TypeDefinition type) [0x00000] in :0
at Mono.Cecil.MetadataBuilder.AddTypeDefs () [0x00000] in :0
at Mono.Cecil.MetadataBuilder.BuildTypes () [0x00000] in :0
at Mono.Cecil.MetadataBuilder.BuildModule () [0x00000] in :0
at Mono.Cecil.MetadataBuilder.BuildMetadata () [0x00000] in :0
at Mono.Cecil.ModuleWriter.b__0 (Mono.Cecil.MetadataBuilder builder, Mono.Cecil.MetadataReader _) [0x00000] in :0
at Mono.Cecil.ModuleDefinition.Read[MetadataBuilder,MetadataBuilder] (Mono.Cecil.MetadataBuilder item, System.Func3 read) [0x00000] in <filename unknown>:0* *at Mono.Cecil.ModuleWriter.BuildMetadata (Mono.Cecil.ModuleDefinition module, Mono.Cecil.MetadataBuilder metadata) [0x00000] in <filename unknown>:0* *at Mono.Cecil.ModuleWriter.WriteModuleTo (Mono.Cecil.ModuleDefinition module, System.IO.Stream stream, Mono.Cecil.WriterParameters parameters) [0x00000] in <filename unknown>:0* *at Mono.Cecil.ModuleDefinition.Write (System.IO.Stream stream, Mono.Cecil.WriterParameters parameters) [0x00000] in <filename unknown>:0* *at Mono.Cecil.ModuleDefinition.Write (System.String fileName, Mono.Cecil.WriterParameters parameters) [0x00000] in <filename unknown>:0* *at Mono.Cecil.AssemblyDefinition.Write (System.String fileName, Mono.Cecil.WriterParameters parameters) [0x00000] in <filename unknown>:0* *at Unity.UNetWeaver.Weaver.Weave (System.String assName, IEnumerable1 dependencies, System.String unityEngineDLLPath, System.String unityUNetDLLPath, System.String outputDir) [0x0017a] in C:\buildslave\unity\build\Extensions\Networking\Weaver\UNetWeaver.cs:1587
at Unity.UNetWeaver.Weaver.WeaveAssemblies (IEnumerable1 assemblies, IEnumerable1 dependencies, System.String outputDir, System.String unityEngineDLLPath, System.String unityUNetDLLPath) [0x00060] in C:\buildslave\unity\build\Extensions\Networking\Weaver\UNetWeaver.cs:1611
UnityEngine.Debug:LogError(Object)
Unity.UNetWeaver.Log:Error(String) (at C:/buildslave/unity/build/Extensions/Networking/Weaver/Program.cs:19)
Unity.UNetWeaver.Weaver:WeaveAssemblies(IEnumerable1, IEnumerable1, String, String, String) (at C:/buildslave/unity/build/Extensions/Networking/Weaver/UNetWeaver.cs:1618)
Unity.UNetWeaver.Program:Process(String, String, String, String[ ], String[ ], Action1, Action1) (at C:/buildslave/unity/build/Extensions/Networking/Weaver/Program.cs:33)
UnityEditor.Scripting.Serialization.Weaver:WeaveUnetFromEditor(String, String, String, String, Boolean)

Followed by a “Failure generating network code.” error. I undid all of the changes I made but this error kept showing up. The biggest problem is that now the compiler will not compile anything else and all of the components have the “The script can’t be loaded” message on them. I reimported all of the scripts, even made a new project and imported the network scripts in it but the same error occured. I know something is wrong with my network code but I commented out everything leaving only the method names (so they can’t be called from outside and won’t produce errors) but this error is still present.

How do I debug this message and find out what’s causing this. I literally have no NetworkBehaviour scripts in existence and the only network code I have has been commented out.

there was a fix for this in a patch release. Try upgrading to the latest editor version.

Sorry, I forgot to mention that I’m with 5.1.2f1. I downloaded it manually from the download page as for some reason checking for updates never finds out if there is a new version. I read in the forums that this was happening with 5.1.1 or 5.1.0.

could you provide an editor log file from when failure occurs?

Yes, of course. I closed Unity, then I ran it again, it compiled the scripts (more like tried to), then I got the error message and closed the editor. So this log should only contain that and a bunch of warnings that I haven’t yet fixed.

One more thing: sometimes when I edit the code the “Failure generating network code” error disappears but then the warnings start appearing as errors.

And lastly I remembered that we switched to Visual Studio the day before this happened. However there were no problems with the compilation that day.

Hope this information helps!

2249282–150266–Editor.zip (19.4 KB)

The editor log says you at version “5.1.2f1”. You should try the latest patch release of the editor (currently 5.1.2p3)

There logs shows a number of MessageBase derived classes, and one “Player” class. Could you comment those out and see if the error goes away?

I just updated to 5.1.2p3 and that did not help. The Player class derives from MonoBehaviour and I have no idea why the compiler thinks it’s deriving from NetworkBehaviour. It was deriving before but in the course of commenting everything out I made it derive from MonoBehaviour again.

Now here’s the interesting part. I commented out all the MessageBase classes and the error disappeared and everything compiled. Then I removed the comments and the error appeared again. What I have basically is a .cs file that just holds a bunch of message classes all deriving from MessageBase. So I scouted through the file uncommenting the classes one by one and I found out that the error seems to appear because of this one:

```csharp
**public class PlayerDataMessage : MessageBase{

public PlayerData info = new PlayerData ("", 0, 0);

}**
```

The PlayerData class holds all the information about a player: name, score, ID, money, etc. It also holds a NetworkConnection and this is some code from the class:

The properties:

[System.Serializable]
public class PlayerData{
    public string playerName = "";
    public int team = 0;
    public int score = 0;
    public int money = 0;
    public int rank = 0;
    public int unlockedLevel = 0;
    public int currentShip = 0;
    public int id = -1;  //this is a multiplayer value for platyer identification

    public List<ShipConfiguration> hangar = new List<ShipConfiguration>();

    public bool canUploadMusic = false;
    public bool inLobby = false;

    [System.NonSerialized]
    public string savePath = ""; //this gets set upon load and does not save
    [System.NonSerialized]
    public NetworkConnection connection = null; //this is a multiplayer value for player connection
    [System.NonSerialized]
    public GameObject playerObject = null;
....

The constructor I’m using

public PlayerData(string name, int team, int unlockedLevel){
this.playerName = name;
        this.team = team;
        this.score = 0;
        this.rank = 0;
        this.unlockedLevel = unlockedLevel;
        this.currentShip = 0;
        this.money = 0;
        this.hangar = new List<ShipConfiguration>();
        this.id = -1;
        this.connection = null; //NetworkConnection
        playerObject = null; //GameObject
}

You can see what types of variables and how many they are. I tried declaring the info property in the PlayerDataMessage as a null or with an empty constructor which does nothing:
public PlayerData info = null;
public PlayerData info = new PlayerData ();
But I still got Failure to generate network code.

So bottom line, I’m guessing that using an object of type PlayerData in a MessageBase class breaks the code. I can transfer this with a byte array, that won’t be a problem for me but I’m curious to know why does this happen.

Thank you for the help! I’d still be stuck without your intervention!

UNet uses a different serialization system, which apparently fails in an unexpected way when processing the types in your PlayerData class. It is probably the “public List hangar” member variable.

Generic lists are not supported. It should have generated an error instead throwing an unhelpful exception later. I’ll file a bug on this.

Thank you for your support, I appreciate that a lot! I will be serializing this class into a byte array myself and that should keep me “out of trouble” :slight_smile:

I got around dealing with unserializeable types by just implementing my own serialize/deserialize methods. In serialization, it writes the number of items in the list and then each list item in turn. In deserialization, it reads that, and then loops to read all of the written entries from the message. At the very least, it avoids creating the temporary byte array.

Out of curiosity Sean, do you have any metrics for UNet’s compression algorithm vs just writing raw data? Perhaps a comparison to other potential compression solutions, such as Google’s protobuf?

Exactly, that is what I have too. I ran the PlayerData object through my serializer and sent it and received it successfully! I have no compression algorithms though and that is a thing I may want to look at.

I’m not entirely sure if we’re on the same page; I was talking about the message “Serialize()” and “Deserialize()” overrides :smile: