How do I despawn NetworkObject with no stateAuthority (Shared Mode)

SlattBurger
edited August 2022 in Fusion

Hello,

How do I despawn NetworkObject that has been in the scene before joining? I have tried calling Runner.Despawn() from MasterClient but it does not works.

Comments

  • Hi,

    This should be the correct approach, since the MasterClient has state authority over the scene objects.

    Do you get any console errors? How are you despawning the Object? Could you provide the code snippet? What Fusion SDK version are you using? Could you try again with the last nightly?

    -----

    Isaac Augusto

    Photon Fusion Team

  • Hello,

    • No, I do not get any console errors.
    • I am calling Runner.Despawn() from MasterClient"
    • Code:
    private void Update() => PickUpItemHandler();
    
    private void PickUpItemHandler()
    {
        Ray ray = GetComponentInChildren<Camera>().ScreenPointToRay(new Vector2(Screen.width / 2, Screen.height / 2));
        if (Physics.Raycast(ray, out RaycastHit hit))
        {
            PickupableItem pItem = hit.collider.GetComponentInParent<PickupableItem>();
    
            if (pItem && Input.GetKeyDown(KeyCode.F))
            {        
                print($"Object's authority = {pItem.GetComponent<NetworkObject>().StateAuthority}"); // prints "[Player:None]"
                print($"State authority is LocalPlayer = {pItem.GetComponent<NetworkObject>().StateAuthority == FindObjectOfType<NetworkRunner>().LocalPlayer}"); // prints "false"
                print($"LocalPlayer is MasterClient = {FindObjectOfType<NetworkRunner>().IsSharedModeMasterClient}"); // prints "true"
    
                FindObjectOfType<NetworkRunner>().Despawn(pItem.GetComponent<NetworkObject>());
            }
        }
    }
    
    • I am using 1.1.1 Stable
    • No, it still does not works on 1.1.2 Nightly


    I have also tried to do the same thing with object that was spawned by this player and it works fine.

    Is there any option that would allow clients to destroy object without stateAuthority over that object?

  • Isaac_Augusto
    edited August 2022

    Hi,

    Despawn() and Spawn() shouldn't be called outside Fusion loops (FixedUpdateNetwork, Runner callbacks, etc) to prevent unexpected behaviours, please try the same in FixedUpdateNetwork instead of Update.

    Is there any option that would allow clients to destroy object without stateAuthority over that object?

    No, that would be against the idea of the state authority.

    -----

    Isaac Augusto

    Photon Fusion Team

  • Found out I got this error because NetworkObject I was trying to destroy was not registered in NetworkRunner, so my problem is solved. Thank you for your time.