- Home /
Destroying Netowork Player on Disconnect
Hello, Does anyone know if there is a way to access a GameObject with:
function OnPlayerDisconnected(player : NetworkPlayer) { Debug.Log("Clean up after player " + player); Network.RemoveRPCs(player); Network.DestroyPlayerObjects(player); }
I Don't have my network script attached to a player, I have it attached to a Empty GameObject Called Network Manager.
I wanted to know if i could somehow make
player : NetworkPlayer = GameObject.Find(Cloned Prefab)
Without any errors
Answer by Khada · Mar 14, 2013 at 01:43 PM
The way I solve this, is to store the NetworkPlayer and PlayerPrefab in a Dictionary with one acting as Key, and the other as Value (I actually keep two dictionaries, the seconds having the Key and Value types reversed). Part of how I do this is to create the PlayerPrefab instance inside the OnPlayerConneted callback, storing it as a Value, with the NetworkPlayer argument as the Key.
This way I can just use the received/sent NetworkPlayer from an RPC to easily grab the corresponding GameObject.
Oh, you're in JavaScript (boo!) which (to my knowledge) doesn't have a built-in C#-style Dictionary container. The easiest thing might be to use 2 Unity JavaScript Arrays.
In short, when a player joins, add the NetworkPlayer to one array and the GameObject to the other, and when the player leaves, remove from both. Then you can loop through the array of NetworkPlayers, find the NetworkPlayer that matches the one sent in an RPC and use it's index value to obtains the corresponding GameObject from the other list.
well then...I will start looking up tutorials then because i have no idea what You just said xD yep ima noob.
If you're new, might I suggest (and other will disagree) that you learn C# rather than JS. In some ways C# can be considered harder but I would say that overall it's easier. It's harder to mess things up because it's a statically typed language as apposed to JS which is dynamically typed, and it has easier-to-use built in libraries (which give you access to things like Dictionaries) and a nicer syntax (though that's personal opinion).
Either way, I would definitely get a solid grounding in single player games before moving onto multiplayer as multiplayer games are notably more complex.
I'll leave it up to your judgement whether or not you think I've answered your question (and thus whether or not to mark it as an answer etc). :)
Answer by ExTheSea · Mar 14, 2013 at 02:38 PM
I use this http://docs.unity3d.com/Documentation/ScriptReference/Network.DestroyPlayerObjects.html but I think this will destroy every Gameobject instantiated by the Player who disconnected.
Your answer
![](https://koobas.hobune.stream/wayback/20220613094411im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to synchronize GameObjects 2 Answers
Synchronizing Game State Via RPCs 0 Answers