Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Networking NetworkBehaviours cannot use commands from parent classes

Discussion in '5.1 Beta' started by CodeBison, Apr 28, 2015.

  1. CodeBison

    CodeBison

    Joined:
    Feb 1, 2014
    Posts:
    288
    Commands in NetworkBehaviours cannot be used in subclasses of those NetworkBehaviours. Here's an example:

    Given this baseic NetworkBehaviour:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class CmdTest : NetworkBehaviour {
    6.  
    7.     protected void Test() {
    8.         if (!isServer) {
    9.             CmdCmdTest();
    10.         }
    11.     }
    12.  
    13.     [Command]
    14.     void CmdCmdTest() {
    15.         Debug.Log("CmdTest!");
    16.     }
    17.  
    18. }
    and this subclass:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class AdvCmdTest : CmdTest {
    6.  
    7.     void Update() {
    8.         Test();
    9.         AdvTest();
    10.     }
    11.  
    12.     void AdvTest() {
    13.         if (!isServer) {
    14.             CmdAdvTest();
    15.         }
    16.     }
    17.  
    18.     [Command]
    19.     void CmdAdvTest() {
    20.         Debug.Log("AdvTest");
    21.     }
    22. }
    Calling the command in the base class results in a "Found no receiver" message, but the command directly in the subclass executes properly:

    Code (CSharp):
    1. Found no receiver for incoming command [Command:InvokeCmdCmdCmdTest] on Player(Clone) (UnityEngine.GameObject),  the server and client should have the same NetworkBehaviour instances [netId=13].
    2. UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
    Code (CSharp):
    1. AdvTest
    2. UnityEngine.Debug:Log(Object)
    3. AdvCmdTest:CmdAdvTest() (at Assets/_Shooter/Scripts/AdvCmdTest.cs:24)
    4. AdvCmdTest:InvokeCmdCmdAdvTest(NetworkBehaviour, NetworkReader)
    5. UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
     
  2. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    Bug 693939 was filed for this.
     
  3. CodeBison

    CodeBison

    Joined:
    Feb 1, 2014
    Posts:
    288
    This appears to be resolved (Commands get invoked from subclasses), however I'm now seeing an error in the log for each other network aware component on the server's instance of the GameObject when the command gets called from a remote client.

    Sample output for above code with other components on GameObject:

    Code (CSharp):
    1. InvokeCommand class [NetworkTransform] doesn't match [CmdTest])
    2. UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
    3.  
    4. InvokeCommand class [NetworkTransformVisualizer] doesn't match [CmdTest])
    5. UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
    6.  
    7. InvokeCommand class [NetworkTransform] doesn't match [AdvCmdTest])
    8. UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
    9.  
    10. InvokeCommand class [NetworkTransformVisualizer] doesn't match [AdvCmdTest])
    11. UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
    12.  
    Additionally, the ClientRpcs have a similar problem to the initial behaviour regarding commands - attempting to execute a ClientRpc from a base class results in a message like the following:

    Code (CSharp):
    1. Failed to invoke RPC ClientRpc:CmdTest:InvokeRpcRpcBase(-284462125) on netID 2
    2. UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
    3.  
     
    NemoFree likes this.
  4. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    That will happen if you have both CmdTest and AdvTest components (child and parent classes) on the same object. To the system, that is the same as having two instances of the same NetworkBehaviour on the object - and that is not supported.
     
  5. CodeBison

    CodeBison

    Joined:
    Feb 1, 2014
    Posts:
    288
    I don't have both components on the object. Just AdvTest. The errors are complaining about the other components (network transform etc) not matching CmdTest or AdvTest. The reason both are being matched is because AdvTest is using a command from its parent class, CmdTest.
     
  6. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,122
    I just wanted to add that I am also hitting this bug. I have only added the child class to the GameObject. Case 697682 was filed for this
     
    Last edited: May 19, 2015
  7. GTGD

    GTGD

    Joined:
    Feb 7, 2012
    Posts:
    436
    I'm not even using a child class and I'm getting errors like these in the editor.

    Code (CSharp):
    1. InvokeCommand class [Player_PositionSync] doesn't match [Player_NewSynRotation])
    2. UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
    3.  
    4. InvokeCommand class [Player_Id] doesn't match [Player_NewSynRotation])
    5. UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
    6.  
    7. InvokeCommand class [Player_Health] doesn't match [Player_NewSynRotation])
    8. UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
    If I host a game in the editor then I get these messages from the remote client. If I host the game from a standalone build I can connect as a remote client seemingly without any problems. I'm seeing these messages in the 5.1.0f2 build but I don't see them in the 5.1.0b5 build.

    It seems that only one script can derive from NetworkBehaviour on a GameObject. It must also sit above any other scripts on that GameObject and those other scripts must be deactivated. I need to get around to submitting a bug report.

    Bug report case 699269
     
    Last edited: May 25, 2015
  8. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    This error message is a bug (696680), the commands still work. The bug is fixed in the first patch release for 5.1.
     
  9. GTGD

    GTGD

    Joined:
    Feb 7, 2012
    Posts:
    436
    Out of curiosity, when should we actually see that kind of error? People have been asking me about it and I've been telling them to ignore it for now.
     
  10. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    when that bug is fixed, the only times you would see that error would be when you have mismatching code between a client and server, or when you remove NetworkBehaviour components from objects at runtime.
     
    GTGD likes this.