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)
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);
}
}