play an AudioSource multipe times???

it’s an example to show the problem.

  public AudioSource a;

    void Update(){
        if(Input.GetKeyDown(KeyCode.F)){
                a.Play();
        }
    }

    }

so i have an audioSource.when i push F key,it plays the audio.but the problem is that if I push F when the audio is playing it will stop the audioSource and play it again at first.how can I fix this problem???any solution?(it should play the audio multipe times)

Do you want the audio to be overlapped or just not cut-off?

instead of doing what you currently do, you can create a prefab GameObject that plays a sound and then destroys itself, like this code:

public class SoundPlayer: MonoBehaviour {
    AudioSource as;
    void Start(){
        as = GetComponent<AudioSource>();
        as.Play();
        Destroy(gameObject);
    }
}

And in the code you used, you can do this:

public GameObject prefab;    //this should be the GameObject prefab that has the code above

void Update(){
    if(Input.GetKeyDown(KeyCode.F)){
        Instantiate(prefab, transform.position, Quaternion.identity);
    }
}

thanks dude but i found the answer ,it is very easier if I use an audioClip.but thanks for your answer :slight_smile:

yes.but I found the answer,thanks :slight_smile:

https://answers.unity.com/questions/1497119/play-an-audiosource-multipe-times.html