Search Unity

Errors trying to destroy objects

Discussion in 'Connected Games' started by amherst, Jul 17, 2009.

  1. amherst

    amherst

    Joined:
    Dec 9, 2008
    Posts:
    30
    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"
    Code (csharp):
    1.  
    2. networkView.RPC("killCube", RPCMode.AllBuffered);
    3.  
    The RPC itself looks like this:
    Code (csharp):
    1.  
    2. @RPC
    3. function killCube ( ) {
    4.     Destroy(theCube);
    5. }
    6.  
    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.
    2. 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.
    Code (csharp):
    1.  
    2. networkView.RPC("recursiveKill", RPCMode.AllBuffered, vid);
    3.  
    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.
    Code (csharp):
    1.  
    2. @RPC
    3. function recursiveKill(vid : NetworkViewID) {
    4.     for (var go in FindObjectsOfType(GameObject)) {
    5.         if (go.networkView) {
    6.             if (go.networkView.viewID == vid) {
    7.                 if (go.transform.childCount > 0) {
    8.                     for (var child in (go.transform)) {
    9.                         var childVid = child.networkView.viewID;
    10.                         networkView.RPC("recursiveKill", RPCMode.AllBuffered, childVid);
    11.                     }
    12.                 }
    13.                 Network.RemoveRPCs(go.transform.networkView.viewID);
    14. //              Network.Destroy(go);
    15.                 Destroy(go);
    16.             }
    17.         }
    18.     }
    19. }
    20.  
    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!!
     
  2. LukeB

    LukeB

    Joined:
    Aug 8, 2009
    Posts:
    88
    Hey :)
    if the destroy script is running on the object to be destroyed, then Destroy(gameObject); in your RPC function should work :)

    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 :)
    Code (csharp):
    1.  
    2. function OnKill () {
    3. NetworkView.RPC("SendKill",RPCMode.All,NetworkView.ViewID);
    4. }
    5. @RPC
    6. function SendKill (objID: ViewID) {
    7. if (NetworkView.ViewID==objID){
    8. Destroy(gameObject);
    9. // Dont send out another RPC here!
    10. }}
    11.  
    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):

    Code (csharp):
    1.  
    2. function OnKill () {
    3. NetworkView.RPC("SendKill",RPCMode.All);
    4. }
    5. @RPC
    6. function SendKill () {
    7. Destroy(gameObject);
    8. // Dont send out another RPC here!
    9. }
    10.  
     
  3. Ashkan_gc

    Ashkan_gc

    Joined:
    Aug 12, 2009
    Posts:
    918
    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