Weird network spawning bug.

About a week ago I started investigating Unity’s networking system and did quite some progress but I don’t want to go any further until I solve this strange behavior;

I created a pretty simple scene with a floor and a little cube, it has 4 different spawning spots from where the player’s cubes come from when clicking the “Spawn” button.

Everything is fine about creating a game and having other players connect, all this achieved by using the MasterServer to register any games that are created.

The problem comes when a client disconnects and then rejoins the same game room.
When this happens, the host of the game sees everything normally, but the other user who just rejoined will see an older copy of his cube even if he did not yet hit the “spawn” button. Seems like the host is holding some kind of residual instance information which the client interprets as an order to spawn cubes that are not actually there, also checked those mystery cubes’ NetworkView and they are not owned by neither host nor client…

I think there might be some way to absolutely rid the host’s room of any data related to the player that just disconnected, but using Network.DestroyPlayerObjects(player) doesnt seem to be enough. I also tried with something like this:

void OnPlayerDisconnected(NetworkPlayer player) 
  foreach(NetCubeContoller ncc in FindObjectsOfType(typeof(NetCubeContoller)))

Where NetCubeController is a script attached to each player cube and Terminate() destroys the entire GameObject (not just the script, double-checked for that).

As I said, that works for the player that hosts the game, but the original owner of those cubes will keep getting ghostly instances of his past self each time he reconnects to that same game room.



The “game” can be tested here. Once Unity’s test MasterServer is up again.(seems to be offline right now)
To reproduce the bug simply open two tabs with the game, host in one and wait until the room appears listed below the menu in the other tab.

Once connected(as client), spawn a bunch of times, disconnect and connect again, and there they should be, phantom instances.

Thanks in advance for any advice.

So I finally found what the problem was.

When a player disconnects, not only does the owner of the room have to destroy the instances owned by that player but also the RPC calls, that’s why the orphan cubes spawned as soon as a player reconnected.

Networking is so very much easier in Unity that I overlooked the most basic thing.

void OnPlayerDisconnected(NetworkPlayer player)

Not that it applies to every case, but for now it’s a solution.