Resolved - In case anyone has a similar issue in the future…
The core issue was that I had the NetworkList on an in-scene placed object. To solve the issue, I moved my NetworkVariables into a prefab, which I then instantiated and spawned on the server when the host was created. This ensured that it would sync properly with the clients when they connected, and when the server was shutdown, it destroyed this prefab, removing the existing data in the network variables achieving the desired result.
====================================================================
I have scoured the internet to find the answer to this question but I am struggling mightily. I am working on a multiplayer project and cannot for the life of me figure out how to properly reset a Network List Variable when the host disconnects (just stopping the game in the editor or alt+f4 in build).
Short version - if there are 3 items in the NetworkList (reported on all clients), and the host disconnects, the clients are still reporting 3 items in that list. Even when calling .Clear()
when the host is disconnecting. I am simply trying to clear the network list on all clients so they are able to start fresh when joining or hosting a new game.
Here is how I am initializing the list:
private NetworkList<NetworkPlayerData> _multiplayerDataList = new NetworkList<NetworkPlayerData>();
On Connection Event Listener - this bit works fine and all clients react properly.
private void OnConnectionEvent(NetworkManager arg1, ConnectionEventData arg2)
{
if (arg2.EventType == ConnectionEvent.ClientConnected)
{
NetworkPlayerData npd = new NetworkPlayerData{
ClientId = arg2.ClientId
};
_multiplayerDataList .Add(npd);
}
if (arg2.EventType == ConnectionEvent.ClientDisconnected)
{
for (int i = 0; i < _multiplayerDataList .Count; i++)
{
if (_multiplayerDataList [i].ClientId != arg2.ClientId) continue;
_multiplayerDataList .RemoveAt(i);
}
}
}
OnNetworkDespawn Method - where I am attemtping to clear this NetworkList for all clients.
public override void OnNetworkDespawn()
{
if (IsServer){
_multiplayerDataList.Clear();
}
base.OnNetworkDespawn();
}
I am sure that I am just taking the wrong approach here (perhaps I should be using RPCs?) - any insight would be greatly appreciated.
Thank you!