I have some multiplayer code that it’s giving me a bit of a headache. I’m not synchronizing anything over the network as I only need to transfer data between client and server. Only two players can connect (server + client).
When I set up a server and a client connects to it, some prefabs are instantiated in both client and server:
void OnConnectedToServer()
{
print("Server Joined");
// ASK THE SERVER FOR ALL PARAMETERS NEEDED FOR OTHER PARTY RECONSTRUCTION
nV.RPC("Request", RPCMode.Server);
isConnected = true;
}
[RPC]
void Request(NetworkMessageInfo info)
{
print("SERVER: I just got a request from a client that wants to meet with me");
// spawn units in SERVER
_enemyUnitOne = (GameObject)Instantiate(enemyUnitOne, _posOne, Quaternion.identity);
_enemyUnitTwo = (GameObject)Instantiate(enemyUnitTwo, _posTwo, Quaternion.identity);
_enemyUnitThree = (GameObject)Instantiate(enemyUnitThree, _posThree, Quaternion.identity);
_enemyUnitFour = (GameObject)Instantiate(enemyUnitFour, _posFour, Quaternion.identity);
// trigger enemy units spawn in CLIENT
nV.RPC("PlaceUnits", info.sender);
}
That’s good so far. The PlaceUnits function looks like this:
[RPC]
void PlaceUnits()
{
print("CLIENT: placing units...");
_enemyUnitOne = (GameObject)Instantiate(enemyUnitOne, _posOne, Quaternion.identity);
_enemyUnitTwo = (GameObject)Instantiate(enemyUnitTwo, _posTwo, Quaternion.identity);
_enemyUnitThree = (GameObject)Instantiate(enemyUnitThree, _posThree, Quaternion.identity);
_enemyUnitFour = (GameObject)Instantiate(enemyUnitFour, _posFour, Quaternion.identity);
print("CLIENT: units placed.");
}
All of this works perfect. The problem happens when the server clicks on a “Disconnect” button inside OnGUI() that triggers the following:
// SERVER DISCONNECT BUTTON
if (isInitialized)
{
if (GUI.Button(new Rect(Screen.width/2, 500, 200, 200), "Disconnect", style))
{
// destroy enemy prefabs on SERVER
Destroy(_enemyUnitOne);
Destroy(_enemyUnitTwo);
Destroy(_enemyUnitThree);
Destroy(_enemyUnitFour);
// trigger destruction of enemy prefabs on CLIENT
print ("SERVER: ASKING CLIENT TO DESTROY ITS ENEMY PREFABS...");
nV.RPC("DestroyEnemiesInClient", RPCMode.Others);
}
}
And the DestroyEnemiesInClient function:
[RPC]
void DestroyEnemiesInClient()
{
print ("CLIENT: DESTROYING ENEMIES...");
Network.Destroy(_enemyUnitOne);
Network.Destroy(_enemyUnitTwo);
Network.Destroy(_enemyUnitThree);
Network.Destroy(_enemyUnitFour);
print ("CLIENT: ALL ENEMIES DESTROYED.");
// signal SERVER that it is SAFE to DISCONNECT
nV.RPC("TriggerDisconnection", RPCMode.Server);
}
In this last function I tried first with Destroy(instance) and Network.Destroy(instance) but no luck, the objects never get destroyed yet all prints show that the functions are being called correctly in both server and client.
I’d be grateful if anyone could give me some directions as to what I could be doing wrong
Best,
Pzeronow