Skip to content

NGO v1.12.0 & v1.11.0 does not trigger OnNetworkDespawn function on NetworkObjects associated with Disconnecting Clients on the Server side when INetworkPrefabInstanceHandler is used.Β #3190

@Xeynt

Description

@Xeynt

Description

NGO v1.12.0 & v1.11.0 does not trigger OnNetworkDespawn function on NetworkObjects associated with Disconnecting Clients on the Server side when INetworkPrefabInstanceHandler is used.

Reproduce Steps

  1. Use SpawnAsPlayerObject with INetworkPrefabInstanceHandler
  2. Disconnect a Client from the Hosting Server
  3. Monitor the OnNetworkDespawn function on the Player script. It does not execute on the Server when the Client disconnects.

Upgrade to NGO v2.1.1

  1. Repeat the above steps.
  2. Monitor the OnNetworkDespawn function which triggers on the Server now for the player objects associated with the disconnecting client.

It doesn't seem like I'm the only one experiencing this issue (Additionally the Destroy function in INetworkPrefabInstanceHandler is being called twice per NetworkObject, refer to the thread for more details):
https://discussions.unity.com/t/onnetworkdespawn-ondestroy-from-networkbehaviour-does-not-get-executed-on-the-hosting-client-when-using-inetworkprefabinstancehandler/1578473/6

Expected Outcome

OnNetworkDespawn should execute on the Server for NetworkObjects associated with the Disconnecting Clients.

My code:

using System.Collections;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;

public class NetcodeSpawnableHandler : INetworkPrefabInstanceHandler
{
    private readonly SpawnableData spawnableData;

    public NetcodeSpawnableHandler(SpawnableData spawnableData)
    {
        this.spawnableData = spawnableData;
    }

    public void Destroy(NetworkObject networkObject)
    {
        var spawnable = networkObject.GetComponent<Spawnable>();
        if (spawnable != null)
        {
            networkObject.gameObject.SetActive(false);
        }
        else
        {
            Debug.LogWarning("Attempted to despawn an object not managed by SpawnableData.");
        }
    }

    public NetworkObject Instantiate(ulong ownerClientId, Vector3 position, Quaternion rotation)
    {
        Debug.Log("NetcodeSpawnableHandler: " + spawnableData.name);
        var spawnable = spawnableData.Spawn(null, position, rotation);
        return spawnable.GetComponent<NetworkObject>();
    }
}
    public static async Task RegisterSpawnableToNetworkManager(SpawnableData spawnableData)
    {
        await spawnableData.PreloadAssetReference();
        // Add the custom handler for the specified prefab
        NetworkManager.Singleton.PrefabHandler.AddHandler(
            spawnableData.assetReferenceHandle.Result,
            new NetcodeSpawnableHandler(spawnableData)
        );
    }

Screenshots

Environment

  • OS: Windows 11
  • Unity Version: 2023
  • Netcode Version: 1.11.0 and 1.12.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority:highThis issue has high priority and we are focusing to resolve itstat:importStatus - Issue is going to be saved internallytype:bugBug Report

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions