Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Unity 5.2: UNetWeaver error Exception keeps preventing me from testing my project.

Discussion in 'UNet' started by asperatology, Sep 21, 2015.

  1. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    This is the full error log message I got:

    Code (CSharp):
    1. UNetWeaver error: Exception :System.ArgumentException: Member 'System.Int32 UnityEngine.Networking.NetworkConnection::hostId' is declared in another module and needs to be imported
    2.   at Mono.Cecil.MetadataBuilder.LookupToken (IMetadataTokenProvider provider) [0x00000] in <filename unknown>:0
    3.   at Mono.Cecil.Cil.CodeWriter.WriteOperand (Mono.Cecil.Cil.Instruction instruction) [0x00000] in <filename unknown>:0
    4.   at Mono.Cecil.Cil.CodeWriter.WriteInstructions () [0x00000] in <filename unknown>:0
    5.   at Mono.Cecil.Cil.CodeWriter.WriteResolvedMethodBody (Mono.Cecil.MethodDefinition method) [0x00000] in <filename unknown>:0
    6.   at Mono.Cecil.Cil.CodeWriter.WriteMethodBody (Mono.Cecil.MethodDefinition method) [0x00000] in <filename unknown>:0
    7.   at Mono.Cecil.MetadataBuilder.AddMethod (Mono.Cecil.MethodDefinition method) [0x00000] in <filename unknown>:0
    8.   at Mono.Cecil.MetadataBuilder.AddMethods (Mono.Cecil.TypeDefinition type) [0x00000] in <filename unknown>:0
    9.   at Mono.Cecil.MetadataBuilder.AddType (Mono.Cecil.TypeDefinition type) [0x00000] in <filename unknown>:0
    10.   at Mono.Cecil.MetadataBuilder.AddTypeDefs () [0x00000] in <filename unknown>:0
    11.   at Mono.Cecil.MetadataBuilder.BuildTypes () [0x00000] in <filename unknown>:0
    12.   at Mono.Cecil.MetadataBuilder.BuildModule () [0x00000] in <filename unknown>:0
    13.   at Mono.Cecil.MetadataBuilder.BuildMetadata () [0x00000] in <filename unknown>:0
    14.   at Mono.Cecil.ModuleWriter.<BuildMetadata>b__0 (Mono.Cecil.MetadataBuilder builder, Mono.Cecil.MetadataReader _) [0x00000] in <filename unknown>:0
    15.   at Mono.Cecil.ModuleDefinition.Read[MetadataBuilder,MetadataBuilder] (Mono.Cecil.MetadataBuilder item, System.Func`3 read) [0x00000] in <filename unknown>:0
    16.   at Mono.Cecil.ModuleWriter.BuildMetadata (Mono.Cecil.ModuleDefinition module, Mono.Cecil.MetadataBuilder metadata) [0x00000] in <filename unknown>:0
    17.   at Mono.Cecil.ModuleWriter.WriteModuleTo (Mono.Cecil.ModuleDefinition module, System.IO.Stream stream, Mono.Cecil.WriterParameters parameters) [0x00000] in <filename unknown>:0
    18.   at Mono.Cecil.ModuleDefinition.Write (System.IO.Stream stream, Mono.Cecil.WriterParameters parameters) [0x00000] in <filename unknown>:0
    19.   at Mono.Cecil.ModuleDefinition.Write (System.String fileName, Mono.Cecil.WriterParameters parameters) [0x00000] in <filename unknown>:0
    20.   at Mono.Cecil.AssemblyDefinition.Write (System.String fileName, Mono.Cecil.WriterParameters parameters) [0x00000] in <filename unknown>:0
    21.   at Unity.UNetWeaver.Weaver.Weave (System.String assName, IEnumerable`1 dependencies, IAssemblyResolver assemblyResolver, System.String unityEngineDLLPath, System.String unityUNetDLLPath, System.String outputDir) [0x001be] in C:\buildslave\unity\build\Extensions\Networking\Weaver\UNetWeaver.cs:1613
    22.   at Unity.UNetWeaver.Weaver.WeaveAssemblies (IEnumerable`1 assemblies, IEnumerable`1 dependencies, IAssemblyResolver assemblyResolver, System.String outputDir, System.String unityEngineDLLPath, System.String unityUNetDLLPath) [0x00062] in C:\buildslave\unity\build\Extensions\Networking\Weaver\UNetWeaver.cs:1637
    23. UnityEngine.Debug:LogError(Object)
    24. Unity.UNetWeaver.Log:Error(String) (at C:/buildslave/unity/build/Extensions/Networking/Weaver/Program.cs:20)
    25. Unity.UNetWeaver.Weaver:WeaveAssemblies(IEnumerable`1, IEnumerable`1, IAssemblyResolver, String, String, String) (at C:/buildslave/unity/build/Extensions/Networking/Weaver/UNetWeaver.cs:1644)
    26. Unity.UNetWeaver.Program:Process(String, String, String, String[], String[], IAssemblyResolver, Action`1, Action`1) (at C:/buildslave/unity/build/Extensions/Networking/Weaver/Program.cs:34)
    27. UnityEditor.Scripting.Serialization.Weaver:WeaveUnetFromEditor(String, String, String, String, Boolean)
    28.  
    29.  
    30.  
    31. Failure generating network code.
    32. UnityEditor.Scripting.Serialization.Weaver:WeaveUnetFromEditor(String, String, String, String, Boolean)
    33.  
    Does anyone know how I could work around this?
     
  2. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,559
    Seems to be some issue running the weaver on your code. How are you using "hostId" in your script ?
     
  3. Firoball

    Firoball

    Joined:
    Aug 6, 2015
    Posts:
    60
    Reads like you need a "using UnityEngine.Networking;" instead of dot syntax. Maybe because UNet is an extension not part of the engine core. Don't know the real reason, though.
     
  4. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,559
    The error comes from Mono.Cecil (the "reflection" library that UNetWeaver uses).
    It is iterating on some module (contained in an assembly) but it says that the field "hostId" that is defined on NetworkConnection comes from another module (indeed it does - its defined in UnityEngine.Networking.dll).

    The only question is - how are you using this field in your code? The error message is not really informative and doesn't tell you how you're using the code wrong (or where it is failing, in case it is UNetWeaver's fault).
     
  5. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    HostId is in the NetworkConnection class. You are probably trying to do something with a NetworkConnection object that is not appropriate. Better error messages for UNetWeaver issues are being added over time, but it is still not catching all the cases.
     
    liortal likes this.
  6. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Sorry for the vague-ness.

    I was just doing the following code:

    Code (CSharp):
    1. NetworkServer.SpawnWithClientAuthority(spawnPrefab, spawnPrefab.GetComponent<NetworkIdentity>().connectionToClient);
    In the Start() function of an empty NetworkBehaviour class. It was a brand new project, so there's nothing in the Assets folder except for a game object prefab with the script attached and a Network Manager HUD.
     
  7. Firoball

    Firoball

    Joined:
    Aug 6, 2015
    Posts:
    60
    I don't know if this is causing the weaver problem, but for sure a prefab can't deliver the right connection. To my knowledge the whole NetworkIdentity component will only deliver useful values after the object was succesfully spawned.

    You'll need NetworkServer.RegisterHandler to add a handler function on connection of client. There you will get the needed NetworkConnection as parameter.


    ...or just use NetworkManager and have it do all this work for you.

    edit: take a look here: http://docs.unity3d.com/ScriptReference/Networking.NetworkServer.RegisterHandler.html
    NetworkMessage netMsg.conn will have the NetworkConnection you're searching for
     
    Last edited: Sep 21, 2015
    asperatology likes this.
  8. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    It may have been that. I was just following what the documentation said about object spawning with client authority. If that's the actual problem, then I would vouch for the documentation to give a better example or a more coherent version.
     
  9. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    NetworkServer.SpawnWithClientAuthority has nothing to do with UNetWeaver, that is probably not it.
     
  10. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Well, I had submitted a bug report. It's Case 729117. Hopefully it helps.
     
  11. LeopardX

    LeopardX

    Joined:
    May 31, 2015
    Posts:
    64
    I had the exact same thing, I had to close down unity editor and re load my project then it was fine..after removing the culprit code, basically i was passing the networkconnection to a [Command] and it dosent like it, so im sending the connectionid of the networkconnection insted.. now it compiles fine.

    5.2.3.f1
     
    Last edited: Dec 2, 2015
  12. Wriggler

    Wriggler

    Joined:
    Jun 7, 2013
    Posts:
    116
    Yep, I had the same error. Removing the NetworkConnection parameter to a command fixed it. Thanks for the heads up.

    Ben
     
  13. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    fyi sending a NetworkConnection or even a connectionId in a [command] doesn't do anything at all and breaks the security model of commands. The connectionId will always be 1 on a client - it is not a server-side or global ID, it is a local client-side ID. (unless there are multiple connection in that client process). Clients do not - and SHOULD NOT - know about the connection id of themselves or other players on the server.
     
  14. Wriggler

    Wriggler

    Joined:
    Jun 7, 2013
    Posts:
    116
    Thanks for the info seanr. So perhaps I'm using the wrong structure for the task I'm trying to solve. I currently have each client sorting out it's players offline in a series of menus (it's a split-screen game). On the final menu when all players are setup the client then calls some Command on the server which adds player objects for each of the split screen players.

    I'm passing the NetworkConnection from ClientScene.readyConnection.connectionId, and ultimately using that in a call to NetworkServer.AddPlayerForConnection() on the server. Is that bad? Is there a better practice for the client to add multiple player objects?

    Apologies if this is basic stuff... it's quite easy to get tangled up in the web of UNet at the moment.

    Thanks for your help!

    Ben
     
  15. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    can you post your server code for adding a player?
     
  16. Wriggler

    Wriggler

    Joined:
    Jun 7, 2013
    Posts:
    116
    Sure, it's something like this...

    Code (CSharp):
    1.  
    2. [Command]
    3. public void CmdAddNewPlayerToRace(int networkConnectionId, short playerControllerId, int characterIndex, int colourIndex)
    4. {
    5.     NetworkConnection networkConnection = NetworkServer.connections[networkConnectionId];
    6.     addNewCarToRace(networkConnection, playerControllerId, characterIndex, colourIndex);
    7. }
    8.  
    9. //Note: this function is used for adding any car to the race, whether it is human or AI controlled.
    10. public GameObject addNewCarToRace(NetworkConnection networkConnection, short localPlayerIndex, int characterIndex, int colourIndex)
    11. {
    12.     if (!isServer) { return null; }
    13.    
    14.     //...snip...
    15.    
    16.     GameObject prefabToInstantiate = gameSettings.characterCarPrefabs[characterIndex];
    17.     GameObject newCar = Instantiate(prefabToInstantiate) as GameObject;
    18.    
    19.     //...snip...
    20.    
    21.     if (isHuman)
    22.     {
    23.         //If this connection already has a player object then we need to replace it here
    24.         if (getExistingPlayerObjectForConnection(networkConnection, localPlayerIndex) != null)
    25.         {
    26.             NetworkServer.ReplacePlayerForConnection(networkConnection, newCar, localPlayerIndex);
    27.         } else {
    28.             //...Otherwise, just add a new player object
    29.             NetworkServer.AddPlayerForConnection(networkConnection, newCar, localPlayerIndex);
    30.         }
    31.     } else {
    32.            
    33.         //Find a car with server authority and spawn the AI with it's authority
    34.         foreach (GameObject car in cars)
    35.         {
    36.             RaceCarSettings carSettings = car.GetComponent<RaceCarSettings>();
    37.             if (carSettings.isServer && carSettings.hasAuthority)
    38.             {
    39.                 NetworkServer.SpawnWithClientAuthority(newCar, car);
    40.             }
    41.         }
    42.     }
    43.    
    44.     return newCar;
    45. }
    46.  
     
  17. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    that wont work with more than 2 players. every remote client will report NetworkConnectionId of 1.

    On the server the base class member variable on NetworkBehaviour "connectionToClient" is the connection object for that player.
     
  18. Wriggler

    Wriggler

    Joined:
    Jun 7, 2013
    Posts:
    116
    Hmm, that variable always appears to be null for me.

    Code (CSharp):
    1.  
    2. [Command]
    3.     public void CmdAddNewPlayerToRace(short playerControllerId, int characterIndex, int colourIndex)
    4.     {
    5.         //this.connectionToClient is always null
    6.         addNewPlayer(this.connectionToClient, playerControllerId, characterIndex, colourIndex);
    7.     }
    8.  
    connectionToServer is always null too. Is this because I'm testing as the host, and thus there isn't a connection to itself? If that's the case, what is the host supposed to do with regard to spawning player objects? Where do I get the NetworkConnection object from?

    Thanks again for your help.

    Ben
     
  19. Wriggler

    Wriggler

    Joined:
    Jun 7, 2013
    Posts:
    116
    After reading a bit more on this property, it appears to only be valid for player objects on the server. But the code hasn't got as far as to have created the player objects yet... I'm using the NetworkConnection to actually make the player objects (with a call to NetworkServer.AddPlayerForConnection()). What's the best practice here?

    Thanks seanr,

    Ben
     
  20. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    CmdAddNewPlayerToRace is not on a player object?

    If it is a client-authority non-player object there is GetComponent<NetworkIdentity>().clientAuthorityOwner
     
  21. Wriggler

    Wriggler

    Joined:
    Jun 7, 2013
    Posts:
    116
    Nope, this is all on a RaceManager object. Every client has one of these objects, and key variables (like number of laps) are kept in sync with SyncVars. The version of the RaceManager that exists on the server is also responsible for spawning cars and other server-only race actions. The RaceManager isn't a player object (that's the car object itself). I haven't set it up to have any special authority - I've just thrown a NetworkIdentity on it and communicate with Commands. So I do something like...

    Code (CSharp):
    1. RaceManager.instance.CmdAddNewPlayerToRace((short)playerIndex, charSelScreen.playerCharacterSelections[i], playerSelectedColourIndex[i]);
    Is that bad? Should I assign client authority to each individual RaceManager? Is there a better technique to manage multiplayer sessions like this?

    This is also null on my RaceManager object, so I guess I'm not setting it up correctly or assigning the correct authority...

    Ben
     
  22. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
  23. Wriggler

    Wriggler

    Joined:
    Jun 7, 2013
    Posts:
    116
    Ah, I see. It's obvious to me now that I need to reconsider the layout of this RaceManager object, and have it exist with authority on the server. I'm going to hack things to pieces today and hopefully end up with a better structure. Thanks for your help.

    Ben