Errors trying to destroy objects

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:

  1. 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.

  1. 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!!

Hey :slight_smile:
if the destroy script is running on the object to be destroyed, then Destroy(gameObject); in your RPC function should work :slight_smile:

gameObject is the current object, whilst GameObject is another object in the scene.
It also looks like your above code will result in a never ending loop (using up tons of memory, slowing down the game), so heres a lil bit of untested code - hopefully it should help you out a bit :slight_smile:

function OnKill () {
NetworkView.RPC("SendKill",RPCMode.All,NetworkView.ViewID);
}
@RPC
function SendKill (objID: ViewID) {
if (NetworkView.ViewID==objID){
Destroy(gameObject);
// Dont send out another RPC here!
}}

Edit - as far as I am aware you probably won’t even need to send the ID and do a check as RPC functions only go between that ID (if this script is running on the object being destroyed):

function OnKill () {
NetworkView.RPC("SendKill",RPCMode.All);
}
@RPC
function SendKill () {
Destroy(gameObject);
// Dont send out another RPC here!
}

1 GameObject is not another object in the scene and is the name of the class and can be used to call static functions and variables
2 i have this problem with destroy too and now i will send it to unity bug report system