Search Unity

Resolved [Netcode] Trying to receive NamedMessage from client 0 which is not in a connected state.

Discussion in 'Netcode for GameObjects' started by unity_7E6DEB89F1AD73F69A8D, Nov 29, 2023.

  1. unity_7E6DEB89F1AD73F69A8D

    unity_7E6DEB89F1AD73F69A8D

    Joined:
    Nov 18, 2023
    Posts:
    2
    I'm sorry if this is a silly problem to be having but I cannot seem to find any solution to this warning message anywhere. I am currently following a tutorial for getting starting with Netcode for GameObjects (
    ). For reference I am 18:28 into the video, but it has been really simple setup so far. I have a Network Manager setup with buttons to be Server/Host/Client, a player prefab with a NetworkObject component, PlayerNetwork script that has very basic movement, and a Network Transform component that should allow me to sync this movement between the client and server.

    When I start as a Host I am able to move the player around and it logs:
    StartHost

    Initialize

    Client Connected

    [Netcode] [Server-Side Client-Synchronization] NetworkObject serialization order:Player(Clone)Player(Clone)


    However when I start as a Client in a separate instance I am not able to move the player around at all and it logs:
    StartClient

    Initialize

    Connected

    [Netcode] Trying to receive NamedMessage from client 0 which is not in a connected state.


    I really am not sure what is happening because I even retraced all my steps and I cannot find any issues.

    For clarification, here are my only 2 scripts:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using Unity.Netcode;
    4. using UnityEngine;
    5.  
    6. public class PlayerNetwork : NetworkBehaviour
    7. {
    8.     private void Update()
    9.     {
    10.         if (!IsOwner) return;
    11.  
    12.         Vector3 moveDir = new Vector3(0, 0, 0);
    13.  
    14.         if (Input.GetKey(KeyCode.W)) moveDir.z = +1f;
    15.         if (Input.GetKey(KeyCode.A)) moveDir.z = -1f;
    16.         if (Input.GetKey(KeyCode.S)) moveDir.x = -1f;
    17.         if (Input.GetKey(KeyCode.D)) moveDir.x = +1f;
    18.  
    19.         float moveSpeed = 3f;
    20.         transform.position += moveDir * moveSpeed * Time.deltaTime;
    21.     }
    22. }
    23.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using Unity.Netcode;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class NetworkManagerUI : MonoBehaviour
    8. {
    9.     [SerializeField] private Button serverBtn;
    10.     [SerializeField] private Button hostBtn;
    11.     [SerializeField] private Button clientBtn;
    12.  
    13.     private void Awake()
    14.     {
    15.         serverBtn.onClick.AddListener(() =>
    16.         {
    17.             NetworkManager.Singleton.StartServer();
    18.         });
    19.  
    20.         hostBtn.onClick.AddListener(() =>
    21.         {
    22.             NetworkManager.Singleton.StartHost();
    23.         });
    24.  
    25.         clientBtn.onClick.AddListener(() =>
    26.         {
    27.             NetworkManager.Singleton.StartClient();
    28.         });
    29.     }
    30. }
    31.  
    Here is a screenshot of the inspector of the Client that cannot move (Client is currently the editor, Build is the host). Issue happens regardless of who is client or host. All movement from the host syncs to the client perfectly fine, I just cannot control it.
    upload_2023-11-28_19-44-28.png


    Here is my NetworkManager object:
    upload_2023-11-28_19-47-30.png

    Please let me know what kind of obvious thing I am probably missing. Thank you so much in advance for your time!
     
  2. NoelStephens_Unity

    NoelStephens_Unity

    Unity Technologies

    Joined:
    Feb 12, 2022
    Posts:
    257
    It looks like you might be wanting to use an owner authoritative NetworkTransform (aka ClientNetworkTransform) as opposed to the default server authoritative (i.e. just the normal NetworkTransform component).
    You have a few options:
    • Create your own derived child class from NetworkTransform then:
      • Override OnIsServerAuthoritative and just return false
    • Copy and paste/use the documentation version
    • Or use this version that I whipped up for you that combines your motion into the NetworkTransform itself (my personal preference on handling motion, as it can be advantageous down the road having your motion tied to the same component updates that handle your transform synchronization).
    Code (CSharp):
    1.  
    2. using Unity.Netcode;
    3. using Unity.Netcode.Components;
    4. using UnityEngine;
    5. #if UNITY_EDITOR
    6. using UnityEditor;
    7. // This bypases the default custom editor for NetworkTransform
    8. // and lets you modify your custom NetworkTransform's properties
    9. // within the inspector view
    10. [CustomEditor(typeof(BaseAuthorityMotion), true)]
    11. public class BaseAuthorityMotionEditor : Editor
    12. {
    13. }
    14. #endif
    15.  
    16. /// <summary>
    17. /// Base authority motion handler that defaults to
    18. /// owner authoritative mode.
    19. /// </summary>
    20. public class BaseAuthorityMotion : NetworkTransform
    21. {
    22.     public enum AuthorityModes
    23.     {
    24.         Owner,
    25.         Server
    26.     }
    27.  
    28.     public AuthorityModes AuthorityMode = AuthorityModes.Owner;
    29.  
    30.     [Range(0.5f, 10.0f)]
    31.     public float MoveSpeed = 3.0f;
    32.  
    33.     private Vector3 m_MoveDir;
    34.  
    35.     protected override bool OnIsServerAuthoritative()
    36.     {
    37.         return AuthorityMode == AuthorityModes.Server;
    38.     }
    39.  
    40.     private bool IsAuthority()
    41.     {
    42.         return AuthorityMode == AuthorityModes.Owner ? IsOwner : IsServer;
    43.     }
    44.  
    45.     /// <summary>
    46.     /// Is only invoked for the authority, and I went ahead and made it
    47.     /// protected and virtual in the event you wanted to derive from this
    48.     /// class and use it for both player and AI related motion.
    49.     /// Just placed your player input script in here so you can quickly
    50.     /// test the component.
    51.     /// </summary>
    52.     protected virtual void AuthorityUpdate()
    53.     {
    54.         m_MoveDir = Vector3.zero;
    55.  
    56.         if (Input.GetKey(KeyCode.W)) m_MoveDir.z = +1f;
    57.         if (Input.GetKey(KeyCode.A)) m_MoveDir.z = -1f;
    58.         if (Input.GetKey(KeyCode.S)) m_MoveDir.x = -1f;
    59.         if (Input.GetKey(KeyCode.D)) m_MoveDir.x = +1f;
    60.  
    61.         transform.position += m_MoveDir * MoveSpeed * Time.deltaTime;
    62.     }
    63.  
    64.     protected override void Update()
    65.     {
    66.         // The authority side of NetworkTransform.Update returns early and does not
    67.         // apply Interpolation but does dictate & synchronize changes to the transform.
    68.         // Only update the authority when the associated NetworkObject is considered spawned.
    69.         if (IsSpawned && IsAuthority())
    70.         {
    71.             AuthorityUpdate();
    72.             return;
    73.         }
    74.  
    75.         // Always invoke base class when overriding NetworkTransform.Update as non-authority
    76.         // instances handle updating their transform to the authority's updates (which includes
    77.         // interpolating towards transform state updates).
    78.         base.Update();
    79.     }
    80. }
    81.  
    If you choose to use the above BaseAuthorityMotion component then:
    • Create a new script file in your project called: BaseAuthorityMotion.cs
    • Copy the above script and replace the entire contents of BaseAuthorityMotion.cs with it (save changes)
    • Write down or remember your current NetworkTransform settings (I typically just take a screen grab of it)
    • Remove the NetworkTransform component
    • Add the BaseAuthorityMotion component
    • Configure it to have the same NetworkTransform settings
      • The inspector view will look slightly different, but the property names are the same.
    • Save and test

    Let me know if this helps you resolve your issue?
     
  3. unity_7E6DEB89F1AD73F69A8D

    unity_7E6DEB89F1AD73F69A8D

    Joined:
    Nov 18, 2023
    Posts:
    2
    Your custom solution works like a charm! I will definitely do some more reading on the owner authoritative NetworkTransform. Thank you very much for your fast and detailed response, I really appreciate it.
     
    NoelStephens_Unity likes this.
  4. Hewesuk

    Hewesuk

    Joined:
    Dec 6, 2023
    Posts:
    1
    I too managed to get the same part of the video, and came across the same error.

    The video obviously used a slightly older version of unity which obviously doesn't give the exact same error messages. However, the problem is the same one address in the very next part of the video, and is almost identical to the one given above by Noel Stephens
     
    NoelStephens_Unity likes this.