Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Third Party [Fishnet] Script calling order issue

Discussion in 'Multiplayer' started by buckeyes622, Dec 3, 2023.

  1. buckeyes622

    buckeyes622

    Joined:
    Aug 15, 2019
    Posts:
    2
    he ServerRpc is called whenever any player deletes an item they are holding. My problem is that ServerManager.Despawn and holdObject.DestroySelf (which is just Destroy(gameObjecT), are called before the [ObserverRpc] script can finish. So the NetworkObject is null by the time the function can finish (edited)

    So the order of the Debug.logs are, First, Forth, Second, NullReference


    Code (CSharp):
    1.    
    2. public class GameMultiplayer
    3. {
    4. public void DestroyHoldObject(HoldObject holdObject)
    5.     {
    6.         DestroyHoldObjectServer(holdObject);
    7.     }
    8.  
    9.     [ServerRpc(RequireOwnership = false)]
    10.     private void DestroyHoldObjectServer(HoldObject holdObject)
    11.     {
    12.         Debug.Log("First");
    13.         ClearHoldObjectOnParentObserver(holdObject);
    14.  
    15.         Debug.Log("Forth");
    16.         ServerManager.Despawn(holdObject.NetworkObject);
    17.         holdObject.DestroySelf();
    18.     }
    19.  
    20.     [ObserversRpc]
    21.     private void ClearHoldObjectOnParentObserver(HoldObject holdObject)
    22.     {
    23.         Debug.Log("Second");
    24.         holdObject.ClearHoldObjectOnParent();
    25.         Debug.Log("Third");
    26.        
    27.     }
    28.  
    29. }
    30.  
    31. public class HoldObject {
    32.  
    33.     public void DestroySelf()
    34.     {
    35.         Destroy(gameObject);
    36.     }
    37. }
    38.  
    39.  
     
  2. Punfish

    Punfish

    Joined:
    Dec 7, 2014
    Posts:
    383
    You don't need to call destroy if despawning. The despawn method has an overload that lets you specify if to destroy or send to the object pool, default being destroy.

    But you can keep your original behavior as well with a small alteration. I assume the error only occurs as clientHost? If so this is because you are destroying the object before the ObserverRpc goes through. All you need to do is add a null check in the ObserverRpc code.

    Code (CSharp):
    1.   holdObject?.ClearHoldObjectOnParent();
    or...
    Code (CSharp):
    1. if (holdObject != null)
    2.   holdObject.ClearHoldObjectOnParent();