Audio is playing multiple times

So I made a C# script for audio but some reason the audio is playing like 20 times and echos. I don’t think it’s a script problem but figured I would post it here to double check. Is it because the keydown is in my update? Any idea what may be wrong if it’s not the script?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Audio : MonoBehaviour
{

public AudioClip audioClip;
    AudioSource audioSource;

void Start()
    {
        audioSource = GetComponent<AudioSource>();
    }

void Update()
{
  if (Input.GetKeyDown("k"))
        {
        AudioLeft();
        }
}

public void AudioLeft()
     {
           if (audioClip) audioSource.PlayOneShot(audioClip, 0.5F);
      }
}

Are you sure you only have one copy of this script in your entire scene?

PlayOneShot allows overlapping sounds. Play won’t work unless the isPlaying bool is false.
Also, when you specify a key you use the KeyCode class:

if (Input.GetKeyDown(KeyCode.K))

yes this is the only copy

Does it play because you press the key again and again or does it play 20 times on its own (shouldn’t be happening).
If it’s a matter of playing because you’re pressing the key, that’s how it’s supposed to work.

Perhaps you can say what you are trying to accomplish more explicitly?

1 Like

Solved. It was playing 20 times and echoing after I hit the button. I just had to make a bool and set it on and off for isPlaying.