- Home /
RPC is called but it doesn't destroy some GameObject instances
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
Answer by hbalint1 · May 02, 2015 at 08:21 AM
Why don't you Netwrok.Instantiate() the objects with master client? so eveyrone will see them, and don't need to instantiate them locally. Also when someone disconnects, the master server could destroy them with Network.Destroy()
Yeah. That's the approach I will eventually use if there's no way to figure out what's wrong with the other method. Anyway, I understand that creating local prefabs on both Server and Client and then triggering their destruction via RPCs should be working too.
I think it can be a problem that you instantiated the objects locallí but trying to destroy the on network. Network.Destroy() is for destroying objects that were instantiated with Network.Instantiate() method. Try changing your Network.Destroy()s to Destroy()s.
I tried with both Network.Destroy() and Destroy(). But it won't destroy the local prefabs for whatever reason. Could it be a problem with the variable name?
Interesting. I just found an answer on another forum. Are you sure that your objects are active when you trying to destroy them? If you get stuck and you don't $$anonymous$$d, you can share your project. I can take a look on it.
Yeah, it looks like the objects are active before their destruction is called. Alright, I'll see if today after work I have some time to pack it up for you to take a look at.
Your answer
Follow this Question
Related Questions
Networking RPC calls never sent to other clients 1 Answer
network.RPC behavior 1 Answer
client to client ping - NAT punchthrough 1 Answer
Spawning Clients 1 Answer
RPC Call Mix Up Issues 0 Answers