Search Unity

How to destroy objects in a multiplayer game?

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

  1. jorgon

    jorgon

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

    I'm trying to make my first multiplayer game and I've found problems trying to destroy instances of prefabs.

    How can I destroy instances for both players?. If I create an instance of a prefab, how can I destroy it?...

    Here you are my code:

    Code (csharp):
    1. var rainbow:GameObject;
    2. var rainbow_sound: AudioClip;
    3. private var activate_rainbow:boolean=false;[/COLOR][COLOR=navy]
    4.  
    5. private var timerStart : float;[/COLOR] [COLOR=navy]
    6. var timerDelay : float = 5;
    7.  
    8. private var tiempo: float=0;[/COLOR] [COLOR=navy]
    9.  
    10. function Awake ()
    11.  {[/COLOR] [COLOR=navy]
    12.       timerStart = Time.time;
    13.   }
    14.  
    15. function Update ()[/COLOR] [COLOR=navy]
    16. {
    17.  
    18.    print(Time.time);[/COLOR] [COLOR=navy]
    19.  
    20.    if(Network.isServer)[/COLOR] [COLOR=navy]
    21.    {
    22.  
    23.         if (global_counter.activate_rainbow==true)[/COLOR] [COLOR=navy]
    24.         {
    25.          print("enter_activate_rainbow");[/COLOR] [COLOR=navy]
    26.          var myNewTrans : Transform = Network.Instantiate(rainbow, transform.position, transform.rotation,0) as Transform;
    27.          print("instanciated");
    28.          Contador_global.activate_arco_iris=false;
    29.          print("activate_rainbow_false");
    30.          //Get the networkview of this new transform
    31.          //var newObjectsNetworkview : NetworkView = myNewTrans.networkView;
    32.                if (Time.time - timerStart > timerDelay)
    33.                {
    34.                 print("enter_rainbow_destruction");
    35.                 print("networkview before destroying "+networkView.viewID);
    36.                 Network.RemoveRPCs(networkView.viewID);                                  //is it necessary?
    37.                 print("networkview after_destroying "+networkView.viewID);
    38.                 Network.Destroy(gameObject);
    39.                 print("destroyed rainbow");
    40.                 }
    41.  
    42.          }[/COLOR] [COLOR=navy]
    43.  
    44. }[/COLOR] [COLOR=navy]
    45. }[/COLOR]


    This question was located inside another thread which derived into this but I thought that separating from it, could be watched by more people.

    I can instanciate correctly but the destruction doesn't work. All the prints are made ok but in the same second where I instantiate the rainbow: in the same second I can watch my printings related to the instantiation before and after and the destruction (before and after too) meanwhile I'm watching the printing of the time each fraction of second.

    In the tutorials I've been watching always appear ways to destroy players, but not objects gameObjects, etc.

    Any idea?. Any link or tutorial where I could watch how can I do this?.

    Thanks in advance.

    Best regards.