Multiplayer connection approval behaviour

I’m just getting started with netcode for gameobjects and set up a simple connection approval script that handles player spawning. I placed empty game objects throughout the scene with a “SpawnPoint” tag. The script randomly chooses the spawn point and assigns that position to the connecting client’s spawn position. This works when clients connect, but does not work when the host starts it. The host always ends up spawning at 0,0,0. Any idea why this is?

using UnityEngine;
using Unity.Netcode;
public class ConnectionApprovalHandler : MonoBehaviour
{
    private const int MaxPlayers = 10;
    [SerializeField] private GameObject[] spawnPoints;

    private Vector3 getSpawnPoint()
    {
        spawnPoints = GameObject.FindGameObjectsWithTag("SpawnPoint");
        return spawnPoints[Random.Range(0, spawnPoints.Length - 1)].transform.position;
    }

    private void Start()
    {
        NetworkManager.Singleton.ConnectionApprovalCallback = ApprovalCheck;
    }
    private void ApprovalCheck(NetworkManager.ConnectionApprovalRequest request, NetworkManager.ConnectionApprovalResponse response)
    {
        Debug.Log("Connect Approval");
        response.Approved = true;    
        response.CreatePlayerObject = true;
        response.PlayerPrefabHash = null;
        response.Position = getSpawnPoint();
                                     
        if(NetworkManager.Singleton.ConnectedClients.Count >= MaxPlayers)
        {                            
            response.Approved = false;
            response.Reason = "Server is full.";
        }

        response.Pending = false;
    }
}

I have the same problem.

I start a server and when i connect with the first client he is always spawning on position 0,0,0.

The second client(player) is spawning at a correct spawning point. It seems that always the first player ends up on position 0,0,0…

Is there a solution for that???

I also have this problem. I have no idea what @illeinvest is trying to say.

Why dont use an framework like fishnet to do it for you?

Because Netcode for GameObjects advertises that it already has this functionality: https://docs-multiplayer.unity3d.com/netcode/current/basics/connection-approval/#networkmanagerconnectionapprovalresponse

Has anyone found the solution? I’m having the same problem.

Im also having the same problem, it doesnt seem as if its been implemented

I had this issue, make sure that your player prefab Network Object has ‘Transform synchronization’ enabled

Random.Range(0, spawnPoints.Length - 1)

This never includes the last spawn point. Fix to this >>

Random.Range(0, spawnPoints.Length)