From 646cd786af5204c1af87d8853d9895b3e5ce833e Mon Sep 17 00:00:00 2001 From: LPLafontaineB Date: Mon, 5 Dec 2022 15:46:16 -0500 Subject: [PATCH] despawning enemies instead of destroying them --- Basic/Invaders/Assets/Scripts/PlayerBullet.cs | 7 ++++++- Basic/Invaders/Assets/Scripts/SuperEnemyMovement.cs | 8 ++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/Basic/Invaders/Assets/Scripts/PlayerBullet.cs b/Basic/Invaders/Assets/Scripts/PlayerBullet.cs index 5f2e3c89a..25a2e76fe 100644 --- a/Basic/Invaders/Assets/Scripts/PlayerBullet.cs +++ b/Basic/Invaders/Assets/Scripts/PlayerBullet.cs @@ -36,7 +36,12 @@ private void OnTriggerEnter2D(Collider2D collider) { owner.IncreasePlayerScore(hitEnemy.score); - Destroy(hitEnemy.gameObject); + if (NetworkManager.Singleton.IsServer) + { + // Only the server can despawn a NetworkObject + hitEnemy.NetworkObject.Despawn(); + } + Destroy(gameObject); // this instantiates at the position of the bullet, there is an offset in the Y axis on the diff --git a/Basic/Invaders/Assets/Scripts/SuperEnemyMovement.cs b/Basic/Invaders/Assets/Scripts/SuperEnemyMovement.cs index 90dd63d93..0bec46745 100644 --- a/Basic/Invaders/Assets/Scripts/SuperEnemyMovement.cs +++ b/Basic/Invaders/Assets/Scripts/SuperEnemyMovement.cs @@ -1,23 +1,23 @@ using Unity.Netcode; using UnityEngine; -public class SuperEnemyMovement : MonoBehaviour +public class SuperEnemyMovement : NetworkBehaviour { private const float k_YBoundary = 14.0f; // The constant speed at which the Bullet travels [Header("Movement Settings")] [SerializeField] - [Tooltip("The constant speed at which the Sauce moves")] + [Tooltip("The constant speed at which the Saucer moves")] private float m_MoveSpeed = 3.5f; private void Update() { - if (!NetworkManager.Singleton.IsServer) return; + if (!IsServer) return; if (transform.position.x > k_YBoundary) { - if (NetworkManager.Singleton.IsServer) Destroy(gameObject); + if (IsServer) NetworkObject.Despawn(); return; }