Spawn() and late joiners

So as far as I understood from the documentation, the function Spawn should make that all late joiners also instantiate the object. But currently I can only make it work for players already connected to the server.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Netcode;

public class PrepareGameplay_Server : MonoBehaviour
{
    [HideInInspector] public int quantityOfRooms;
    [HideInInspector] public int decksPerRoom;
    [SerializeField] GameObject deckPrefab;

    void Start()
    {
        if (NetworkManager.Singleton.IsServer)
        {
            for (int i = 0; i < quantityOfRooms; i++)
            {
                for (int j = 0; j < decksPerRoom; j++)
                {
                    GameObject deck = Instantiate(deckPrefab);
                    deck.transform.SetParent(this.transform);
                    deck.GetComponent<Deck>().currentRoom.Value = i;
                    deck.GetComponent<NetworkObject>().Spawn(true);
                }
            }
        }
    }
}

Am I doing something wrong here?

Hi,

What jumps out at me is line 21 where you're modifying the parent of the GameObject prior to spawning the NetworkObject over the network.

Modifications of a NetworkObject's parent should always go through the NetworkObject.TrySetParent API as detailed here: https://docs-multiplayer.unity3d.com/netcode/current/advanced-topics/networkobject-parenting. You would want to ensure that the GameObject that you're parenting the deck to is also a NetworkObject, and that you perform this operation after spawning the NetworkObject.

Assuming the deck NetworkObject is registered under your NetworkManager's list of NetworkPrefabs, the rest should still work, and Netcode will automatically synchronize the server's NetworkObjects to the late-joining client.

Are there any warnings or logs produced on the late-joining client you're able to share? I hope that helps!