[POSSIBLE UNITY BUG] AudioSource plays by itself like a machine gun (code isn't even called)

Ok, so a very weird thing is happening. If I wait without clicking anywhere, nothing happens as expected. However, if I fire a few shots (click a few times), or even if I fire one and wait a few seconds, the gun fire sounds will go into a crazy loop and it will sound like a machine gun or fireworks without me clicking at all. I added a breakpoint to the only PlayOneShot call and the strangest thing is it wasn’t being called at all, so I have ABSOLUTELY NO IDEA how it could be that the sound is being played at all, let alone this frequently or how I would even debug beyond this.

I have an AudioSource game object in my hierarchy with an AudioSource attached to it. It is the child of a GameManager game object with a “GameManager” script that has a slot for an AudioSource variable and an AudioClip variable. In the inspector, I assigned the sound I want to play to the AudioClip and this AudioSource game object to the AudioSource slot.

In my GameManager script, in the Update function, I test whether the left mouse button was pressed down and play the sound if it was. This is my code:

bool test = Input.GetMouseButtonDown(0);

        if (test)                                        //if shot has been fired
        {
            if (SHOOTING == currentMode)
            {
                audioSource.PlayOneShot(gunFire);
            }
        }

This is literally the only time in any of my script files that the PlayOneShot() function is called. There is absolutely no other reference to sounds or anything related at all anywhere else. This is completely beyond me and I think it is probably a Unity bug, so below is a link to my project files.

Thanks a lot for your help!

https://ufile.io/ucu4d (31.8MB)

  1. The pasted code is not enough to go on.

  2. Don’t expect people to download your entire project.

  3. I did anyways just to be nice… your project isn’t even set up to example your problem. Your GameManager exists, but the AudioSource is not set up, nor is the sound effect placed in its spot.

  4. Using 9_mm_gunshot-mike-koenig-123 everything works fine for me

  5. I noticed your other gunshot sfx have multiple gunshot sounds in the recording. Note that PlayOneShot does not play just one sound from the clip. It plays the ENTIRE clip once. If that clip contains multiple shots, it’s going to play all of them. The word “Shot” in “PlayOnShot” is not referring to the “shot” sound in your clip.

  6. Almost the entirity of your project, including scripts, exist inside a folder named ‘Resources’. Note that unity uses the folder name ‘Resources’ to identify things that should be force included in the build. DO NOT place everything (especially not scripts) in a folder named ‘Resources’.

1 Like

Thank you very much for your help and time! Please forgive me, I certainly did not mean to be imposing. I was just completely at a loss as to what to do. Thanks a lot!