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
void OnPlayerDisconnected(NetworkPlayer player)
{
Debug.Log("Clean up after player " + player);
Network.RemoveRPCs(player);
Network.DestroyPlayerObjects(player);
}
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!
I copy what you wrote above, I control both of them.However, It didn’t do anything. I still have that player.
I copy what you wrote above, I control both of them.However, It didn’t do anything. I still have that player.
Have a look there http://unity3d.com/support/documentation/ScriptReference/Network.DestroyPlayerObjects.html
That might be your solution.
HI all…
I got the working solution for the issue
Just add the below code to the update method of the player script
Note:- This is for player script which is attached to that player which is controlled by individual persons.
//In C#
void Update ()
{
if (networkView.isMine && (Network.isClient || Network.isServer))
{
if (Input.GetKey(KeyCode.Escape))
{
Network.Destroy (this.gameObject);
Network.Disconnect();
}
}
}
//In JS
function Update ()
{
if (networkView.isMine && (Network.isClient || Network.isServer))
{
if (Input.GetKey(KeyCode.Escape))
{
Network.Destroy (this.gameObject);
Network.Disconnect();
}
}
}
For me, it is working.
public override void OnServerDisconnect(NetworkConnection conn)
{
base.OnServerDisconnect(conn);
// Your other cleanup code here
}
You need to call the base function which does a lot of cleanup for you.
update for photon in 5.5 -----
public class disconncthandler : Photon.MonoBehaviour {
void Update()
{
if (!photonView.isMine && (Network.isClient || Network.isServer))
{
if (Input.GetKey(KeyCode.Escape))
{
Network.Destroy(this.gameObject);
Network.Disconnect();
}
}
}
}// add Photon. in front of MonoBehaviour and use !photonView.isMine