I made small test project but I can't get player object also

This is my whole code of test project but GetPlayerObject() return null in client

what is wrong about this code and what did I miss

help me please

I want to get playerobject in client to

private NetworkRunner runner;

    [SerializeField]
    private GameObject playerObj;

    private void Start()
    {
        runner = gameObject.AddComponent<NetworkRunner>();
        runner.ProvideInput = true;
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.F1))
        {
            Debug.LogError("Host");

            var args = new StartGameArgs();
            args.SessionName = "a";
            args.GameMode = GameMode.Host;
            args.SceneManager = gameObject.AddComponent<NetworkSceneManagerDefault>();

            runner.StartGame(args);
        }
        if (Input.GetKeyDown(KeyCode.F2))
        {
            Debug.LogError("Client");

            var args = new StartGameArgs();
            args.SessionName = "a";
            args.GameMode = GameMode.Client;

            runner.StartGame(args);
        }
        if (Input.GetKeyDown(KeyCode.F3))
        {
            Debug.LogError("ShutDown");

            runner.Shutdown();
        }
        if (Input.GetKeyDown(KeyCode.F4))
        {
            foreach (var player in runner.ActivePlayers)
            {
                runner.SetPlayerObject(player, runner.GetPlayerObject(player));
                Debug.LogError($"{player}, {runner.GetPlayerObject(player)}");
            }
        }
        if (Input.GetKeyDown(KeyCode.F5))
        {
            foreach (var player in runner.ActivePlayers)
            {
                Debug.LogError($"{player}, {runner.GetPlayerObject(player)}");
            }
        }
    }
    public void OnPlayerJoined(NetworkRunner runner, PlayerRef player)
    {
        var obj = runner.Spawn(playerObj, new Vector3(UnityEngine.Random.Range(-5f, 5f), 0, 0), null, player);
        runner.SetPlayerObject(player, obj);
        Debug.LogError($"{player}, {runner.GetPlayerObject(player)}");
    }

Answers

  • I think the problem is that the runner.Spawn works only on the Server, and it always returns null when it is called on the client since only StateAuthority (Host or Server) can spawn objects in the network.