Rpc and NetworkVariable dosent work after PlayerPrefab respawned

Here is the simple test scene:
PlayerPrefab would destroy itself after 5 seconds

    public class SelfDestroy : NetworkBehaviour
    {
        [SerializeField] private float delay = 5f;

        public static event Action<ulong> OnSelfDestroy;

        public override void OnNetworkSpawn()
        {
            // since there is a host and a client, it's saying client would destroy self
            if (IsServer && !IsOwner)
                Destroy(gameObject, delay);
        }

        public override void OnNetworkDespawn()
        {
            OnSelfDestroy?.Invoke(OwnerClientId);
        }
    }

Test Object would respawn it after 0.5 second:

    public class Respawn : NetworkBehaviour
    {
        [SerializeField] private GameObject playerPrefab;

        public override void OnNetworkSpawn()
        {
            if (IsServer)
                SelfDestroy.OnSelfDestroy += RespawnPlayer;
        }

        private void RespawnPlayer(ulong clientId)
        {
            Debug.Log($"Reborn player {clientId}");
            StartCoroutine(RespawnPlayer(0.5f, clientId));
        }

        private IEnumerator RespawnPlayer(float waitTimeSeconds, ulong clientId)
        {
            yield return new WaitForSeconds(waitTimeSeconds);

            var player = Instantiate(playerPrefab, transform.position, transform.rotation);
            player.GetComponent<NetworkObject>().SpawnAsPlayerObject(clientId);
        }
    }

It also has a NetworkVariable and would call TestClientRpc every 2.5 seconds:

    public class Testing : NetworkBehaviour
    {
        public NetworkVariable<int> count = new();

        [SerializeField] private float waitTime = 2.5f;
        private float timer = 2.5f;

        private void Update()
        {
            if (!IsServer) return;

            timer -= Time.deltaTime;
            if (timer > 0f) return;
            
            timer = waitTime;
            TestClientRpc();

            count.Value++;
        }

        [ClientRpc]
        private void TestClientRpc()
        {
            if (!IsServer)
                Debug.Log("Test Client");
        }
    }

But after PlayerPrefab respawned, the NetworkVariable wont synchronize and the TestClientRpc is never called.

I tested this and it does work (I’ve attached the package). You’ll need to re-arrange the code to clean up on client disconnect so that it doesn’t try to respawn the player for the client.
selfdestroy.unitypackage (5.7 KB)

1 Like

Thanks for you testing! But I didnt explain it quite right: both the Testing script and the Respawn script are attached to the Respawn Object. I changed it a little bite and it still case the same problem.
selfdestroy.unitypackage (5.7 KB)

You’re right, there is a problem so I dug a little deeper into it. When the client’s Player is destroyed it’s also removing the client as an observer on other network objects. This happens in version 2.0.0-pre.4, 1.11 has no such issue. I’d highly recommend reporting it as a bug here.

1 Like

Thank you very much! I switched to 1.11.0 version and the problem is totaly gone.The issue is reported here.

1 Like

Just a tip: architect your game so that it is not necessary to destroy a network object when you know that you will need it again soon (respawn).

I suppose you destroy the player because it died. Put it in a “died” state instead, perhaps disable the renderer or disable the Avatar and replace it with a non-networked ragdoll. You may still want to use that player “alive” (from the object perspective) to communicate with the server and other clients. It’s just cleaner this way.

1 Like