Edit:
Hey everyone,
I’ve come across an issue when trying to sync a list of NetworkObjectReference for late-joining clients with Distributed Authory.
For context, each player spawns a number of “ShooterBase” network objects from OnNetworkSpawn.
What I am trying to do is save those spawned network objects to a NetworkList using NetworkObjectReference so that new clients would be able to read them via TryGet and initialize their local List by using a simple GetComponent(). This local list is used for handling the syncing of the weapon visual by disabling and enabling the ShooterBase component.
The problem is, it works for the session owner (both the session owner and client’s List are populated), but not for the client that just joined (the client’s List is populated but not the session owner’s).
So I did a little debugging and found out that turns out the Network Object IDs are different between the session owner and the client.
Session Owner Log:
Client Log
Is this natural? If so, what’s a better way to synchronize a list of network objects for late joining player?
Thanks!
Here is the code for spawning the weapons:
PlayerWeaponHandler.cs
public NetworkList<NetworkObjectReference> weaponNetworkObjects =
new NetworkList<NetworkObjectReference>(null,
NetworkVariableReadPermission.Everyone, NetworkVariableWritePermission.Owner);
private List<ShooterBase> shooters = new List<ShooterBase>(4);
public override void OnNetworkSpawn()
{
base.OnNetworkSpawn();
currentReadiedIndex.OnValueChanged += OnReadiedIndexChanged;
for(int i = 0; i < weaponsToSpawn.Count; i++)
{
if (IsOwner)
{
//Instantiate weapon object
GameObject spawnedWeapon = Instantiate(weaponsToSpawn[i], gripAnchor.position, gripAnchor.rotation, gripAnchor);
//Add to owner local List<ShooterBase>
ShooterBase spawnedShooter = spawnedWeapon.GetComponent<ShooterBase>();
shooters.Add(spawnedShooter);
//Network spawn weapon object
NetworkObject spawnedNetworkObject = spawnedShooter.NetworkObject;
spawnedNetworkObject.Spawn();
//Adds the spawned network object to NetworkList
weaponNetworkObjects.Add(spawnedNetworkObject);
spawnedNetworkObject.TrySetParent(NetworkObject, false);
}
else //Client reads from NetworkList<NetworkObjectReference>
{
Debug.Log("Network Object Id from NetworkList: " + weaponNetworkObjects[i].NetworkObjectId);
weaponNetworkObjects[i].TryGet(out NetworkObject readNetworkObject, NetworkManager.Singleton);
Debug.Log("Network Object from NetworkList: " + readNetworkObject); //readNetworkObject is null on late client
if (readNetworkObject)
{
shooters.Add(readNetworkObject.GetComponent<ShooterBase>());
}
}
}
InitializeWeaponStates();
}