Skip to content

Commit 2976871

Browse files
fix: despawning enemies instead of destroying them (#74)
despawning enemies instead of destroying them
1 parent b8a5775 commit 2976871

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

Basic/Invaders/Assets/Scripts/PlayerBullet.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ private void OnTriggerEnter2D(Collider2D collider)
3636
{
3737
owner.IncreasePlayerScore(hitEnemy.score);
3838

39-
Destroy(hitEnemy.gameObject);
39+
if (NetworkManager.Singleton.IsServer)
40+
{
41+
// Only the server can despawn a NetworkObject
42+
hitEnemy.NetworkObject.Despawn();
43+
}
44+
4045
Destroy(gameObject);
4146

4247
// this instantiates at the position of the bullet, there is an offset in the Y axis on the

Basic/Invaders/Assets/Scripts/SuperEnemyMovement.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
using Unity.Netcode;
22
using UnityEngine;
33

4-
public class SuperEnemyMovement : MonoBehaviour
4+
public class SuperEnemyMovement : NetworkBehaviour
55
{
66
private const float k_YBoundary = 14.0f;
77

88
// The constant speed at which the Bullet travels
99
[Header("Movement Settings")]
1010
[SerializeField]
11-
[Tooltip("The constant speed at which the Sauce moves")]
11+
[Tooltip("The constant speed at which the Saucer moves")]
1212
private float m_MoveSpeed = 3.5f;
1313

1414
private void Update()
1515
{
16-
if (!NetworkManager.Singleton.IsServer) return;
16+
if (!IsServer) return;
1717

1818
if (transform.position.x > k_YBoundary)
1919
{
20-
if (NetworkManager.Singleton.IsServer) Destroy(gameObject);
20+
if (IsServer) NetworkObject.Despawn();
2121
return;
2222
}
2323

0 commit comments

Comments
 (0)