Weapon Audio Playing When Swapping Guns

I created a gun script allowing my gun to fire as well as a weapon swap script, but when I swap weapons it plays the noise.

using UnityEngine;
  
public class WeaponSwap : MonoBehaviour
{
  
    [SerializeField] int selectedWeapon = 0;
    
    // Start is called before the first frame update
    void Start()
    {
        SelectWeapon();
    }
  
    // Update is called once per frame
    void Update()
    {
  
        int previousSelectedWeapon = selectedWeapon;
  
        if (Input.GetAxis("Mouse ScrollWheel") > 0)
        {
            if (selectedWeapon >= transform.childCount - 1)
                selectedWeapon = 0;
            else
                selectedWeapon++;
        }
  
        if (Input.GetAxis("Mouse ScrollWheel") < 0)
        {
            if (selectedWeapon <= 0)
                selectedWeapon = transform.childCount - 1;
            else
                selectedWeapon--;
        }
  
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            selectedWeapon = 0;
        }
  
        if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            selectedWeapon = 1;
        }
  
        if (previousSelectedWeapon != selectedWeapon)
        {
            SelectWeapon();
        }
    }
  
    void SelectWeapon()
    {
        int i = 0;
        foreach (Transform weapon in transform)
        {
            if (i == selectedWeapon)
                weapon.gameObject.SetActive(true);
            else
                weapon.gameObject.SetActive(false);
            i++;
        }
    }
}
using UnityEngine;
  
public class Gun : MonoBehaviour
{
    [SerializeField] float damage = 10f;
    [SerializeField] float range = 100f;
    [SerializeField] float fireRate = 15f;
    [SerializeField] float force = 30f;
  
    [SerializeField] AudioClip fireNoise;
    [SerializeField] Camera fpsCam;
    [SerializeField] ParticleSystem muzzleFlash;
    [SerializeField] GameObject impactEffect;
  
    private float nextTimeToFire = 0f;
    public AudioSource source { get { return GetComponent<AudioSource>(); } }
  
    private void Start()
    {
        gameObject.AddComponent<AudioSource>();
        source.clip = fireNoise;
    }
  
    void Update()
    {
        if (Input.GetButtonDown("Fire1") && Time.time >= nextTimeToFire)
        {
            nextTimeToFire = Time.time + 1f / fireRate;
            Shoot();
            PlaySound();
        }
    }
  
    void Shoot()
    {
        RaycastHit hit;
        muzzleFlash.Play();
        if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
        {
  
            Target target = hit.transform.GetComponent<Target>();
            if (target != null)
            {
                target.TakeDamage(damage);
            }
            GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
            Destroy(impactGO, 2f);
        }
    }
    void PlaySound()
    {
        source.PlayOneShot(fireNoise);
    }
}

I tried removing the PlaySound() from the update but that didn’t stop it, so I put it back and tried moving the audio clip selection from Start() to Update() and that didn’t work either.

@beatdown0312

If each weapon is a different gameobject with a Gun behavior attached to it, then the problem is that you’re creating an Audio Source attached to each gun gameobject (in Gun.cs Start).

Then your gun switching script is disabling the gameobjects of whatever weapons are not active which causes the AudioSources of each one of them to be disabled immediately as well.

To solve it simply use one AudioSource attached to an object that isn’t disabled by the weapon swap script.

Or simply have a permanent AudioSource attached to the gameobject where you add the weapon switching script.

Also, adding GetComponent to a getter (get source) is basically the same as using GetComponent and since you’re using it every time you PlaySound() (every time you shoot). Instead create a “source” variable and use that instead.

Just leaving a comment for whom is coming to this page now: check the flag ‘Play On Awake’ in the Audio Source, to me it was selected by default, and every time I switched weapon (activating it) it just “played on awake” the sound effect.
Unflagging it could resolve the problem. Bye