Skip to content

Commit

Permalink
Unsubscribe all GunController delegates on destruction
Browse files Browse the repository at this point in the history
  • Loading branch information
toberge authored and Fueredoriku committed Dec 30, 2024
1 parent 7493661 commit 8acb19b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
27 changes: 24 additions & 3 deletions Assets/Scripts/Augment/GunController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections;
using Mirror;
using UnityEngine;
using UnityEngine.InputSystem;
Expand Down Expand Up @@ -106,11 +105,31 @@ public void Initialize()

private void OnDestroy()
{
UnsubscribeDelegates();
}

public void UnsubscribeDelegates()
{
onInitializeGun = null;
onFire = null;
onFireEnd = null;
onFireNoAmmo = null;
onFireStart = null;
onInitializeBullet = null;
onReload = null;

projectile.OnColliderHit = null;
projectile.OnHitboxCollision = null;
projectile.OnNetworkInit = null;
projectile.OnProjectileInit = null;
projectile.OnProjectileTravel = null;
projectile.OnRicochet = null;

if (!barrelAnimator)
return;

if (HasRecoil)
barrelAnimator.OnShotFiredAnimation -= PlayRecoil;

barrelAnimator.OnShotFiredAnimation -= PlayRecoil;
barrelAnimator.OnShotFiredAnimation -= ShotFired;
barrelAnimator.OnAnimationEnd -= FireEnd;

Expand Down Expand Up @@ -217,6 +236,8 @@ private void RpcFireWithNoAmmo()

private void FireGun()
{
if (stats == null)
return;
if (stats.Ammo <= 0)
{
CmdFireWithNoAmmo();
Expand Down
2 changes: 2 additions & 0 deletions Assets/Scripts/Gamestate/PlayerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -551,11 +551,13 @@ public void SetGunNetwork(Transform offset)

public void RemoveGun()
{
gunController.UnsubscribeDelegates();
for (int i = gunController.transform.childCount - 1; i >= 0; i--)
{
Destroy(gunController.transform.GetChild(i).gameObject);
}
Destroy(gunController.gameObject);
GunOrigin.GetComponent<GunController>().UnsubscribeDelegates();
for (int i = GunOrigin.transform.childCount - 1; i >= 0; i--)
{
Destroy(GunOrigin.transform.GetChild(i).gameObject);
Expand Down

0 comments on commit 8acb19b

Please sign in to comment.