Search Unity

Question PlayerObjects null on server

Discussion in 'Netcode for GameObjects' started by Freakwave, Jun 4, 2023.

  1. Freakwave

    Freakwave

    Joined:
    Dec 10, 2019
    Posts:
    2
    I'm starting to learn Unity's Netcode for online multiplayer games.

    I have a simple 2D scene where players can connect and control their own square. That works fine and everything gets synched between them.

    I want to spawn monsters that get controlled by the server. Now, I get stuck commanding the monsters to walk to the nearest player.

    My idea:
    • Make sure only the server controls the monster (in OnNetworkSpawn)
    • Let the server search for the nearest player (in RecalculateNearestPlayerServerRpc)
    • Move the monster toward the nearest player (in Update)
    My problem is that while trying to get all PlayerObjects the respective variables in the ConnectedClientsListof the NetworkManager is null:

    Code (CSharp):
    1. NetworkManager.Singleton.ConnectedClientsList[0]
    2. {Unity.Netcode.NetworkClient}
    3.    ClientId: 0
    4.    OwnedObjects: Count = 0
    5.    PlayerObject: null
    6. NetworkManager.Singleton.ConnectedClientsList[1]
    7. {Unity.Netcode.NetworkClient}
    8.    ClientId: 1
    9.    OwnedObjects: Count = 0
    10.    PlayerObject: null
    Here is the code for my MonsterController:

    Code (CSharp):
    1. public class MonsterController : NetworkBehaviour
    2. {
    3.    private Rigidbody2D rb;
    4.    private float speed = 10.0f;
    5.  
    6.    void Awake()
    7.    {
    8.        rb = GetComponent<Rigidbody2D>();
    9.    }
    10.  
    11.    public override void OnNetworkSpawn()
    12.    {
    13.        if (!IsServer) enabled = false;
    14.    }
    15.  
    16.    void Update()
    17.    {
    18.        RecalculateNearestPlayerServerRpc();
    19.        var dir = nearestPlayer.transform.position - transform.position;
    20.        rb.velocity = dir * speed;
    21.    }
    22.  
    23.  
    24.    [ServerRpc]
    25.    private void RecalculateNearestPlayerServerRpc()
    26.    {
    27.        float nearestPlayerDistance = float.MaxValue;
    28.        foreach (NetworkClient client in NetworkManager.Singleton.ConnectedClientsList)
    29.        {
    30.            float sqrDistance = (client.PlayerObject.transform.position-transform.position).sqrMagnitude;
    31.  
    32.            if (sqrDistance < nearestPlayerDistance)
    33.            {
    34.                nearestPlayerDistance = sqrDistance;
    35.                nearestPlayer = client.PlayerObject;
    36.            }
    37.        }
    38.    }
    39. }
    I spawn and give ownership like this:

    Code (CSharp):
    1. public class GameManager : NetworkBehaviour {
    2.    [SerializeField] private PlayerController _playerPrefab;
    3.  
    4.    public override void OnNetworkSpawn() {
    5.        SpawnPlayerServerRpc(NetworkManager.Singleton.LocalClientId);
    6.    }  
    7.  
    8.    [ServerRpc(RequireOwnership = false)]
    9.    private void SpawnPlayerServerRpc(ulong playerId) {
    10.        var spawn = Instantiate(_playerPrefab);
    11.        spawn.NetworkObject.SpawnWithOwnership(playerId);
    12.    }
    13.  
    14.    public override void OnDestroy() {
    15.        base.OnDestroy();
    16.        MatchmakingService.LeaveLobby();
    17.        if(NetworkManager.Singleton != null )NetworkManager.Singleton.Shutdown();
    18.    }
    19. }

    What am I doing wrong?
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,988
    There's a player prefab section in NetworkManager, if you leave that empty, no player gets a "player object" associated with it since in this case you are expected to manually spawn player objects. That is what you do but I can't tell you whether you're expected to assign this reference yourself as well. You could try assigning it, and if that is not allowed, you'd have to keep your own list of current players around (Dictionary with clientId as key, player object as value).
     
    Freakwave likes this.
  3. Freakwave

    Freakwave

    Joined:
    Dec 10, 2019
    Posts:
    2
    Thank you for the hint.

    What I was doing is delaying the player spawn from the NetworkManager. I used the player prefab but called the wrong method while spawning.

    Instead of:
    Code (CSharp):
    1.   [ServerRpc(RequireOwnership = false)]
    2.    private void SpawnPlayerServerRpc(ulong playerId) {
    3.        var spawn = Instantiate(_playerPrefab);
    4.        spawn.NetworkObject.SpawnWithOwnership(playerId);
    5.    }
    I spawn with SpawnAsPlayerObject:
    Code (CSharp):
    1.    [ServerRpc(RequireOwnership = false)]
    2.     private void SpawnPlayerServerRpc(ulong playerId) {
    3.         var spawn = Instantiate(_playerPrefab);
    4.         spawn.NetworkObject.SpawnAsPlayerObject(playerId);
    5.     }