Search Unity

How can I delete or destroy an instance of an object IN A MULTIPLAYER GAME?.

Discussion in 'Multiplayer' started by jorgon, Nov 7, 2011.

  1. jorgon

    jorgon

    Joined:
    Aug 10, 2011
    Posts:
    33
    How can I delete or destroy an instance of an object IN A MULTIPLAYER GAME?.

    I know how to do this in a singleplayer game, but I need to know how to do this in a multiplayer game. What else I need?.

    I can instantiate in the right way and I can watch in the server and client version...But the destruction is my problem: My rainbow can't be destroyed. :-(

    My code is based on the tutorial called "M2H_Networking_Tutorial":

    And my code is:

    Code (csharp):
    1. var rainbow:GameObject;
    2. var rainbow_sound: AudioClip;
    3.  
    4. private var activate_rainbow:boolean=false;
    5.  
    6. var timer : float = 2;
    7.  
    8. function Awake () {
    9. timer = Time.time;
    10. }
    11. function Update () {
    12.  
    13.  
    14. //Called on the server only
    15.     if(Network.isServer)
    16.     {  
    17.  
    18.        if (global_counter.activate_rainbow==true)
    19.        {
    20.          var myNewTrans : Transform = Network.Instantiate(rainbow, transform.position, transform.rotation,0) as Transform;
    21.          global_counter.activate_rainbow=false;
    22.          //Get the networkview of this new transform
    23.  
    24.          var newObjectsNetworkview : NetworkView = myNewTrans.networkView;
    25.  
    26.          //Network.Destroy(myNewTrans,10);
    27.          if (Time.time - timer > 2)
    28.          {
    29.           Network.Destroy(gameObject);
    30.          }
    31.        }
    32.     }
    33. }


    Please, HELP! :)
     
  2. Nicholai Pestot

    Nicholai Pestot

    Joined:
    Nov 7, 2011
    Posts:
    1
    Last edited: Nov 7, 2011
  3. jehos

    jehos

    Joined:
    Aug 11, 2011
    Posts:
    27
  4. jorgon

    jorgon

    Joined:
    Aug 10, 2011
    Posts:
    33
    Hi mates.

    I can print all the printings before and after the supposed destruction of my instantiation. So, if I can print them and Unity doesn't "say" me anything...I don't know what's happening.

    I can destroy in both client and server an object non instantiated, an object created from the starting of the game. I click on it (it'sa wall/cube) and make it desappear with the Network.Destroy(GetComponent(NetworkView).viewID) command;

    The same sentences have been added then to the code I've been posting along this thread to see If I can destroy it (how I can destroy a non instantiated dinamically object I tried to re-use a code which works fine in these conditions to see what happens in the conditions I've got problems)...I can't destroy it when I click on it... :-(

    I've changed some things as you can see in this code:

    Code (csharp):
    1. var arco_iris:GameObject;
    2. var sonido_arco_iris: AudioClip;
    3.  
    4. private var activate_arco_iris:boolean=false;
    5.  
    6. private var timerStart : float=0;
    7. var timerDelay : float = 2;
    8.  
    9.  private var tiempo: float=0;
    10.  
    11. function Awake () {
    12.  
    13. }
    14.  
    15. function Update () {
    16.  
    17.     if(Network.isServer)
    18.     {  
    19.        
    20.         if (Contador_global.activate_arco_iris==true)
    21.             {
    22.             timerStart = Time.time;
    23.             print("entra_activate_arco_irirs");
    24.             var myNewTrans : GameObject = Network.Instantiate(arco_iris, transform.position, transform.rotation,0) as GameObject;
    25.             print("activado"); 
    26.             Contador_global.activate_arco_iris=false;
    27.             print("falso");
    28.             //Get the networkview of this new transform
    29.             //var newObjectsNetworkview : NetworkView = myNewTrans.networkView;
    30.             }
    31.         else if (timerStart != 0  Time.time - timerStart > timerDelay)
    32.             {
    33.             timerStart=0;
    34.             print("entra_destrucción_arcoirirs");
    35.             print("networkview antes de borrar es "+networkView.viewID);
    36.             //Network.RemoveRPCs(networkView.viewID);
    37.             print("networkview despues de borrar es "+networkView.viewID);
    38.             Network.Destroy(GetComponent(NetworkView).viewID);
    39.             //Network.Destroy(myNewTrans);     
    40.             print("destruye_arcoiris");
    41.             //timerStart =0;
    42.             }  
    43.            
    44.        
    45.     }
    46. }
    47.     function OnMouseDown ()
    48.     {
    49.         if(Network.isServer)
    50.         {  
    51.            
    52.                 Network.Destroy(GetComponent(NetworkView).viewID);      
    53.                 //NetworkView.RPC("killObject", RPCMode.AllBuffered);
    54.                    
    55.         }
    56.     }

    But I can't destroy it even hitting on it with the left mouse buton.

    This script is attatched to an empty GameObject, which only gives its position...Maybe this could suppose some kind of issue.

    It's the first time I try to destroy an instantiation...I don't know what else do...

    Please, any idea.

    Thank you very much for your well detailed answer jehos and company.

    Please help me.
     
  5. jehos

    jehos

    Joined:
    Aug 11, 2011
    Posts:
    27
    I'm not sure I'm following you correctly, but if you want to destroy over the network non-instantiated objects, I think that's a problem right there. A design problem at least.

    If you're going to destroy objects over the network, then Network.Instantiate them! You can have the server do that, and then just do Network.Destroy. It's definitely going to save you time and trouble, and that's the whole idea of Network.Instantiate: to have the same object on all players.

    I wouldn't try to destroy over the network non-network objects.

    However, if that's really what you want, then you can do it through RPCs. The problem is you need to know which object you need to destroy for each network player (btw, that is exactly what networkViews are for! they have the same ID on all connected players). You can search that object by name, if they are unique and all players share those objects, or by Tag, if those are rare objects that each object can get its own tag.

    Basically, you could just send an RPC saying: "DestroyObjectByName", and pass the name (or tag) as a String in the RPC, and every player that gets it, does a GameObject.Find() and Destroy()s that object.
     
  6. jorgon

    jorgon

    Joined:
    Aug 10, 2011
    Posts:
    33
    Hi Jehos:
    I can destroy with Network.Destroy(with its two variants) command any object which already exists (with its own NetworkView component) via OnMouseDown.

    But when I add the same lines of code Which can destroy a already existing object to my well known script it can't be destroyed even clicking on it...

    I've realised that the NetworkViewID I'm printing is the NetworkViewID of the EmptyObject I'm using as position for the instantiation not the NetworkViewID OF THE CREATED INSTANCE...So...This makes me think that my instance has NO NetworkViewID...I think that I should ADD A NETWORKVIEWID dinamycally every time I create an instance of my prefab. OHH!!.

    SHOULD BE THIS MY PROBLEM??...So, I've got to discover HOW TO ADD A NETWORKVIEW WITH ITS ID TO THE RENCENTLY INSTANCE CREATED ...When I print the networkView.viewID with "print("networkview antes de borrar es "+networkView.viewID);" before trying to destroy the rainbow and after destroy it with "print("networkview despues de borrar es "+networkView.viewID);" the NetworkViewID I watch is the Empty GameObject's one...The instance I've created should have another ID for the network...Shouldn't it?.

    I'm gonna try it again...But first I've got to know how to add a NetworkView with its ID via script to an instance. :). I'm gonna look for it...If I can't find how to do this I will ask you.

    Thanl you.
     
  7. jorgon

    jorgon

    Joined:
    Aug 10, 2011
    Posts:
    33
    Hi mates!.

    I have solved my problem related to the network destruction of AN INSTANCE of a prefab.

    So, problem solved because I've check it with a webclient version and my editor version connected with the masterserver way to connect them...

    I'M HAPPY!.

    Thank you all for your ideas and orientations.
     
    Last edited: Nov 14, 2011
  8. jorgon

    jorgon

    Joined:
    Aug 10, 2011
    Posts:
    33
    Hi again, after achieving the networking instantiation and its right destruction and the destruction of an already existent GameObject...

    I've got now another problem related with massive instantiation of some random GameObjects from a set of GameObjects:

    a).-I need to instantiate a huge number of instances each one inside an update function in a script. I use Network.Instantiate and before this I make an allocate for the viewID like this:

    Code (csharp):
    1. var viewID = Network.AllocateViewID();
    b).-They are instantiated from a grid located in an Empty GameObject. And here I dragged the a).- script

    c).-They are destroyed in another script located in the prefabs taken for each instance. They, the instances of these prefabs are falling continuosly due to the Gravity Force and when they collide against the ground they must be destroyed.

    In the server all it's ok. But it seems that the client has problems to follow the instances...It's like if its frames per second were reduced to 1 per hour!!. But I've checked that if I disconnect the server I can recover the control of my client's game and if I connect it as server I can found all my instances which has fallen from the spwamn grid and hasn't been destroyed being on the ground...

    So two problems:

    a).-Problems with the initial connection.

    b).-Maybe I can't destroy the instances when they collide against the ground.

    Is it due to the sepration of the instantiation and the destruction in different scripts?.

    I've got to say that I'm making tests with a webclient and my server in my Unity's editor window running simultaneously.

    Any idea?. Please help me... ;-)
     
    Last edited: Nov 24, 2011