Hello, I have a problem, I have it set so that when the player touches an enemy, it loads the menu, but when I play again and load the same scene, the gun does not work and it gives me errors the object type of gun has been destroyed an object type of audio source has been destroyed please help me I don’t know what to do Thanks in advance for your help
Gun script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Gun : MonoBehaviour {
[Header("References")]
[SerializeField] private GunData gunData;
[SerializeField] private Transform muzzle;
public static void Main(){}
public Camera WeaponCam;
Animator weaponAnimator;
float timeSinceLastShot;
public ParticleSystem muzzleFlash;
public AudioSource audio;
private void Start()
{
PlayerShoot.shootInput += Shoot;
PlayerShoot.reloadInput += StartReload;
weaponAnimator = GameObject.FindGameObjectWithTag("WeaponHolder").GetComponent<Animator>();
audio = GetComponent<AudioSource>();
}
public void StartReload() {
if (!gunData.reloading) {
StartCoroutine(Reload());
}
}
private IEnumerator Reload() {
gunData.reloading = true;
weaponAnimator.SetBool("reloading", true);
yield return new WaitForSeconds(gunData.reloadTime);
gunData.currentAmmo = gunData.magSize;
gunData.reloading = false;
weaponAnimator.SetBool("reloading", false);
}
private bool CanShoot() => !gunData.reloading && timeSinceLastShot > 1f / (gunData.fireRate / 60f);
private void Shoot()
{
if (gunData.currentAmmo > 0)
audio.PlayOneShot(audio.clip);
if (gunData.currentAmmo > 0)
muzzleFlash.Play();
if (gunData.currentAmmo > 0)
{
if (CanShoot())
{
if (Physics.Raycast(muzzle.position, muzzle.forward, out RaycastHit hitInfo, gunData.maxDistance))
{
Debug.Log(hitInfo.transform.name);
}
gunData.currentAmmo--;
timeSinceLastShot = 0;
OnGunShot();
}
}
}
private void Update() {
timeSinceLastShot += Time.deltaTime;
Debug.DrawRay(muzzle.position, muzzle.forward);
}
void OnDisable() { }
private void OnGunShot() {
}
}