Search Unity

OnDestroy & OnNetworkDestroy & isServer

Discussion in 'Multiplayer' started by any_user, Jun 23, 2015.

  1. any_user

    any_user

    Joined:
    Oct 19, 2008
    Posts:
    374
    I'm trying to figure out how to do something on the server after an object is destroyed. In my case it's a connected (networked/spawned) object that should be unspawned and destroyed when the main object is destroyed.

    The problem is that inside OnDestroy isServer is already set to false on the server, and OnNetworkDestroy is only called on the client (right?). This means there's no way to do something specifically when an object is destroyed on the server.

    Code (CSharp):
    1.  
    2.     // in a NetworkBehaviour
    3.     // this *doesn't* work
    4.     void OnDestroy ()
    5.     {
    6.         if (isServer) // is always false
    7.         {
    8.             // do something on server
    9.         }
    10.     }
    11.  
    12.     public override void OnNetworkDestroy ()
    13.     {
    14.         // do something on client
    15.  
    16.         base.OnNetworkDestroy ();
    17.     }
    18.  
    I have some ideas how I could work around it, but I'm curious what's the "right" way to do that. Either isServer should still be set in OnDestroy or we should have a OnNetworkDestroy function that is called on the client AND on the server.
     
    Daavlot likes this.
  2. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    right, OnNetworkDestroy is only called on clients - including the local client on the host.

    And yes, the NetworkIdentity unsets isServer as part of its OnDestroy, so if your script component's OnDestroy is called after the NetworkIdentity's OnDestroy then isServer will be false.
     
  3. any_user

    any_user

    Joined:
    Oct 19, 2008
    Posts:
    374
    Thanks for confirming. Are there any improvements planned in this area? We have OnStartServer and OnStartClient methods, why not OnDestroyServer and OnDestroyClient?
     
  4. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    The isServer property is true when the NetworkIdentity for the object is active on the server. If you just want to check if your code is running on the server you could use NetworkServer.active, that is not scoped to a particular object.
     
  5. any_user

    any_user

    Joined:
    Oct 19, 2008
    Posts:
    374
    ah yeah, that's true.. wasn't aware that isServer means "is currently spawned and we're on the server", thought it's just a shorthand to check if the script is running on the server.
     
  6. forcepusher

    forcepusher

    Joined:
    Jun 25, 2012
    Posts:
    227
    I don't understand the use of "OnNetworkDestroy" because it's not being invoked while stopping the host, neither when disconnecting from server as a client.
    It's just not good enough for cleanup. I'm using regular OnDestroy for now.
     
    Last edited: May 7, 2016
  7. mercior

    mercior

    Joined:
    Sep 21, 2015
    Posts:
    16
    just adding to this thread to say this issue tripped me up today.

    In my opinion, "isServer" being false on the OnDestroy event should at the least be noted in the official documentation of OnDestroy, and NetworkServer.Destroy. Right now the only way to find this out is to google and find this forum thread.

    Even better would be if the behaviour can be fixed, or we can get OnClientDestroy and OnServerDestroy events.
     
    HadynTheHuman likes this.
  8. bobmoff

    bobmoff

    Joined:
    Sep 23, 2014
    Posts:
    44
    I just bumped in to this issue as well. Would be nice to have some better way to handle this.

    IMHO I think it would be nice if isClient would be accessible in the OnDestroy method.
     
    HadynTheHuman likes this.
  9. HadynTheHuman

    HadynTheHuman

    Joined:
    Feb 15, 2017
    Posts:
    14
    Yeah, I just ran into this as well - and I second everything mercior has suggested!

    As it stands I've just implemented my own persistentIsServer bool which gets set OnStartServer.
     
    Last edited: Mar 14, 2017