Search Unity

Destroy object locally only.

Discussion in 'Multiplayer' started by noise256, Aug 27, 2015.

  1. noise256

    noise256

    Joined:
    Apr 7, 2013
    Posts:
    22
    Hi,

    I was under the impression that to Destroy an object remotely you had to use NetworkServer.Destroy(gameObject) however, I'm calling GameObject.Destroy(gameObect) on the server and it appears to be destroying the object on the client as well.

    Here is my code (bit messy atm):

    Code (CSharp):
    1. public class Collidable : NetworkBehaviour {
    2.     public bool drawHP = false;
    3.     public Texture hpTexture;
    4.  
    5.     [SyncVar]
    6.     public int health = 0;
    7.  
    8.     public bool marked = false;
    9.  
    10.     public void Awake() {
    11.         this.health = GetComponent<GameData>().getHealth();
    12.     }
    13.    
    14.     public void OnTriggerEnter2D(Collider2D collider) {
    15.         if (gameObject.layer == LayerMask.NameToLayer("Static") || collider.gameObject.layer == LayerMask.NameToLayer("Static")) {
    16.             return;
    17.         }
    18.  
    19.         if (collider.gameObject.GetComponent<Ownable>().getOwner() == GetComponent<Ownable>().getOwner()) {
    20.             return;
    21.         }
    22.  
    23.         //HERE
    24.         this.health -= collider.GetComponent<GameData>().getCollisionDamage();
    25.  
    26.         if (base.hasAuthority && this.health <= 0) {
    27.             RpcMark();
    28.             GameObject.Destroy(gameObject);
    29.         }
    30.         else if (marked && health <= 0) {
    31.             Debug.Log ("Destroyed locally"); //Not getting called
    32.             GameObject.Destroy(gameObject);
    33.         }
    34.     }
    35.  
    36.     [ClientRpc]
    37.     public void RpcMark() {
    38.         this.marked = true;
    39.     }
    40.  
    41.     public override void OnNetworkDestroy() {
    42.         Debug.Log ("Destroyed on network."); //Getting called
    43.     }
    44. }
    This seems very strange to me so sorry if I'm being stupid. What I'm trying to do is have the object be destroyed on the server but not destroyed on the client until the same collision (which is delayed because of latency) happens there as well.
     
  2. james69lemon

    james69lemon

    Joined:
    Nov 24, 2013
    Posts:
    6
    I need the same thing,

    Not proud of this answer but it works:
    On collision, disable the renderer, disable the networkTransform, and start a coroutine that destorys it in 0.5 seconds, so all clients are responsible for their own object destruction as long as it collides within 0.5 seconds
     
    basboi likes this.
  3. basboi

    basboi

    Joined:
    Nov 11, 2016
    Posts:
    14
    4 years later, another nub joins the ranks and isnt proud about it either! :)