- Home /
How can I play multiple audioclips from the same object?
Hi! Beginner Unity solo student here. I am only starting to understand the C# programming and void stuff, etc. I have a problem that I have made a rocket boost game; when I press Spacebar, the thrust engine sound starts playing. As soon as It's not pressed anymore, thrust engine sound stops through:
private void StopThrusting()
{
audioSource.Stop();
mainEngineParticles.Stop();
}
However, I would also like to have a Sound effect when rocket hits an item with class "Fuel", I thought using:
AudioSource audioSource;
AudioClip FuelSoundEffect;
public void OnTriggerEnter(Collider other)
{
if(other.gameObject.CompareTag("Fuel")){
CurrentFuel += BurningRate;
FuelSound();
other.gameObject.GetComponent<MeshRenderer>().enabled = false;
other.gameObject.GetComponent<Light>().enabled = false;
}
}
public void FuelSound()
{
audioSource.PlayOneShot(FuelSoundEffect);
}
However the problem here, is that StopThrusting ends the audioSource by calling audioSource.Stop(); when I release Spacebar.
How is it possible to stop only the mainEngine sound effect, but not the FuelSound effect when colliding with "Fuel" item?
Answer by rh_galaxy · May 20, 2021 at 04:39 AM
You can have more than one AudioSource... I have one for each constant/looping sound and one general for all of the one shot sounds since they can play simultaneously.
So in the inspector you add more AudioSources, with an assigned AudioClip or not. Then in Awake() you can test for the clip name and get the AudioSource for each clip and then use that for playing sounds later.
AudioSource oASThruster;
AudioSource oASGeneral;
private void Awake()
{
foreach (AudioSource source in GetComponents<AudioSource>())
{ //we have 2 audio sources
if (source.clip!=null && source.clip.name.Equals("thruster"))
oASThruster = source; //this is used only for thruster
else
oASGeneral = source; //this one is used for all different one shot sounds
}
}
Also a tip for stopping sounds... when you stop a sound abruptly it can introduce a clicking noise. Instead of stopping the sound, I set bThrusterFadeOut to true.
bool bThrusterFadeOut = false;
void Update()
{
//fade out some sounds over ~100ms, to avoid unwanted clicking noise
if (bThrusterFadeOut)
oASThruster.volume *= 0.8f;
}
Answer by Casiell · May 19, 2021 at 08:33 PM
I had the same problem and I solved it by setting clip to null and calling Play. This will stop playing your previous clip and start playing null which is nothing. In my case it worked, because I changed the playing clip constantly anyway.
You may try just pausing the clip, Pause shouldn't affect OneShot clips. But this way the sound won't start from the beginning the next time you play it, so that may be a problem
You could also just have 2 audio sources. One for the sound playing when holding space and another that you use only for one shots. I think this is how I would've done it probably.
Your answer
Follow this Question
Related Questions
Problem with playing sound on Trigger-Enter 2 Answers
How to get decibel's value of game's sound? 0 Answers
Play audio at consistent volume 2 Answers
AudioSource won't play 2 Answers