- Home /
Disconnecting from network doesn't destroy the player.
I am trying to make an online game. Everything seems OK but when a player disconnects from the server, it can't control its player anymore. However, its player stays there still. How can I destroy it? I tried Destroy(); , Destroy(GameObject); , Destroy(this) after disconnecting the player.However, none of them didn't work. (I have a button when the player clicks it disconnects the player. I want his player destroy exactly after that it disconnects from the server. How can I do it? Thanks
Answer by Bladesfist · Jan 07, 2012 at 03:38 PM
void OnPlayerDisconnected(NetworkPlayer player)
{
Debug.Log("Clean up after player " + player);
Network.RemoveRPCs(player);
Network.DestroyPlayerObjects(player);
}
Answer by FuzzyQuills · Sep 01, 2014 at 03:46 AM
Actually, try just doing this:
function OnDisconnectedFromServer () {
Destroy(gameObject);
}
Stick this in the player prefab used to play the game.
EDIT: Corrected the function name. turns out I typed it wrong! ;)
Answer by sanamdehghan · Jan 07, 2012 at 11:39 AM
I copy what you wrote above, I control both of them.However, It didn't do anything. I still have that player.
Who spawns the player object? Network.Destroy(NetworkPlayer);
destroys objects owned by that player, i.e. instantiated by that player over the network or objects that are explicitly set as owned by that player (via networkView.owner = somePlayer
).
Answer by sanamdehghan · Jan 07, 2012 at 11:39 AM
I copy what you wrote above, I control both of them.However, It didn't do anything. I still have that player.
Network.Destroy(PlayerObject.GetComponent().viewID);, it should work, for me works, but there is something else about my problem, when a player disconnects, it destroys on all other clients and server, but when a player join after that, he can see that disconnected player in the past, and the new player is Player 2, i only have player 0 and he comes with player 2, if player 2 disconnects, and a new player joins, he would be player 3, and is able to see player 1 and 2, who left earlier. YEAP, suckss, I'm a mess right now
Answer by fafase · Jan 07, 2012 at 11:48 AM
Have a look there http://unity3d.com/support/documentation/ScriptReference/Network.DestroyPlayerObjects.html
That might be your solution.
Your answer
![](https://koobas.hobune.stream/wayback/20220613062741im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to Destroy Game Object In All Clients in Photon Network 0 Answers
Error: "A client which was not in the connected player list disconnected. ???" 1 Answer
Error destroying player over network 1 Answer
Destroy its own instantiated objects when leaving server 1 Answer
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers