I am writing a networked multiplayer architectural building game/tool in which players create objects, manipulate them, and then might destroy them. I am having trouble with the destroying part and have tried several approaches, all of which have their own serious problem.
When a player begins building something they start with a cube and all other items they create become child objects of the cube. Each child object of the cube can in turn have multiple generations of child objects. The cubes each have two networkViews attached: one for the cube transform (b/c the player will be affecting the transform values of the cube) and one that points to a script attached to the cube that contains all the RPC functions for the cube, including the destroy function below.
I create all the objects with Network.Instantiate. As for destroying I have tried different approaches:
- I call an RPC function (which is in a script attached to the parent cube) to kill the parent cube. I don’t need to pass the cube in as a paramter because it is already declared as a var in the script “theCube”
networkView.RPC("killCube", RPCMode.AllBuffered);
The RPC itself looks like this:
@RPC
function killCube ( ) {
Destroy(theCube);
}
I get two problems here:
-
if a player creates a cube, adds child objects to it, then deletes it, new players joining after player1 deleted the cube don’t see the cube (as expected) but they DO still see all the child object of the cube. I would think that since I destroyed the cube, which is the parent object, all the child objects would disappear as well.
-
If subsequent players create a cube, add child objects to it, then try to delete it, I get the following errors, repeated twice. I only get this error on subsequent players. The first player acts as the game server. What’s weird here is that I don’t actually call Network.Destroy. Instead I call an RPC of Destroy.
- For my second approach I tried by dealing with recursively killing the child objects. I call an RPC function (which is in a script attached to the parent cube) to kill the parent cube and all children. vid is the NetworkViewID of the cube.
networkView.RPC("recursiveKill", RPCMode.AllBuffered, vid);
The RPC itself is below. The line “Network.Destroy(go)” is commented out because I have tried running this script both ways, as Destroy and as Network.Destroy and both yield the same problem. I’ve even tried it with both Destroys, and still the same problem.
@RPC
function recursiveKill(vid : NetworkViewID) {
for (var go in FindObjectsOfType(GameObject)) {
if (go.networkView) {
if (go.networkView.viewID == vid) {
if (go.transform.childCount > 0) {
for (var child in (go.transform)) {
var childVid = child.networkView.viewID;
networkView.RPC("recursiveKill", RPCMode.AllBuffered, childVid);
}
}
Network.RemoveRPCs(go.transform.networkView.viewID);
// Network.Destroy(go);
Destroy(go);
}
}
}
}
The problem I’m getting is that everything works fine on all remote clients, but on the calling client I get the following errors. The parent cube has networkViewID 154 here.
Can anyone offer some advice here? I’m running both players off the same machine. I don’t think that should be the problem but I really don’t know what to make of this. I feel like I’ve tried every possibility and I’m completely stumped.
Thanks a lot!!