OnServerAddPlayer SyncList not initialized

Hey,

I’m struggeling with the SyncList for some hours now.

What I am trying to accomplish: Load player items into a SyncList inside the OnServerAddPlayer before AddPlayerForConnection.

Issue: I’m getting this error on the server side

Here is the code of the uploaded minimal project.

public class MyNetworkManager : NetworkManager {

    public GameObject StartItemPrefab;

    public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId) {
        GameObject player = (GameObject)Instantiate(this.playerPrefab, startPositions[0].transform.position, Quaternion.identity);

        // Load item
        GameObject item = Instantiate(StartItemPrefab);
        NetworkServer.Spawn(item);

        player.GetComponent<Player>().items = new SyncListUInt();
        // Give player the item       
        player.GetComponent<Player>().items.Add(item.GetComponent<NetworkIdentity>().netId.Value);

        NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
    }
}
public class Player : NetworkBehaviour {

    public SyncListUInt items = new SyncListUInt();
   
    public override void OnStartClient() {
        Debug.Log("Item " + items[0]);
    }
}

Thanks in advance for any help!

3045834–228333–SyncListError.rar (3.28 MB)

You are calling “items = new SyncListUInt();” twice (on lines 12 and 3). Try only calling it on line 3.

Wow, I could have sworn I already tried that. But why does this work? Do you have a clue? I mean, it sounds illogical to me that the “not initialized” error vanishes if I delete the line initializing the error throwing object.

Anyhow, thanks heaps!

you were overwriting items and maybe confusing it. Might want to use properties {get; private set;} to protect yourself from accidentally doing things like that.