Rpc and NetworkVariable dosent work after PlayerPrefab respawned

Hello.I changed the project a little bite so I just put a simple test scene to show the problem.
Here is the simple test scene
PlayerPrefab would destroy itself after 4 seconds

public class SpawnPlayer : NetworkBehaviour
    {
        [SerializeField] private float delay = 4f;

        public static event Action<ulong> OnSelfDestroy;

        public override void OnNetworkSpawn()
        {
            // host destroys client player
            if (IsServer && !IsOwner)
                Destroy(gameObject, delay);
        }

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

        public override void OnDestroy()
        {
            Debug.Log($"SpawnPlayer Destroyed: {OwnerClientId}");

            base.OnDestroy();
        }
    }

Test Object would respawn it after 0.1 second:

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

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

        private void RespawnPlayer(ulong clientId)
        {
            StartCoroutine(RespawnPlayer(0.1f, clientId));
        }

        private IEnumerator RespawnPlayer(float waitTimeSeconds, ulong clientId)
        {
            yield return new WaitForSeconds(waitTimeSeconds);
            
            Debug.Log($"Respawn player {clientId}");
            var player = Instantiate(playerPrefab, transform.position, transform.rotation);
            player.GetComponent<NetworkObject>().SpawnAsPlayerObject(clientId);
        }

        public override void OnDestroy()
        {
            Debug.Log($"Respawn Destroyed: {OwnerClientId}");

            if (IsServer)
                SpawnPlayer.OnSelfDestroy -= RespawnPlayer;

            base.OnDestroy();
        }
    }

Test Objectt also has a NetworkVariable and would call TestClientRpc every 2 seconds:

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

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

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

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

            timer = waitTime;

            Debug.Log($"Testing Update Send Client Rpc {OwnerClientId}");

            TestClientRpc();

            count.Value++;
        }

        [ClientRpc]
        private void TestClientRpc()
        {
            if (!IsServer)
                Debug.Log($"Testing TestClientRpc Client: {OwnerClientId}");
        }
    }

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

Version 2.0.0-pre.4 has this bug, just use 1.11.0 instead.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms