- Home /
multiple audio sources playing
I have multiple audio sources each attached to different objects that are supposed to play when the character walks through the collider, but all the audio sources play at once no matter how far away my character is from the objects. how do i fix this so only one audio source plays when the character is in range?
Answer by FeedMyKids1 · Jun 28, 2020 at 06:23 PM
I'm guessing you have something on your objects with the audio sources that reads
if (TriggerClass.instance.collidingWithPlayer == true)
audioSource.Play();
You could instead put a trigger on each object (Just put a sphere collider and set IsTrigger to true) then in a class ON THAT OBJECT write:
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Player"))
audioSource.Play();
}
So the radius of your collider/trigger is the distance that determines if the player is in range and makes sure that only that object's AudioSource component plays and not all of them.
I think a more efficient solution would be to put a single OnCollisionEnter() on the player and then access the audio source of the colliding object with a certain layer and then play it, since your solution would require putting this script onto all the objects which can be quite tedious.
Answer by jones586 · Jul 26, 2021 at 09:30 AM
You can attach several Audio Sources to the same GameObject in the Inspector, and get them to play at the same time by calling PlayOneShot(); in a script. You need the Audio Source attached to your main GameObject and then attach a script to the Audio Source.
Your answer
![](https://koobas.hobune.stream/wayback/20220613002004im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Activating audioclip explosion sound when bullet hits enemy in 2d spaceship game 0 Answers
Audio Listener Problem 5 Answers
audio.Play() not working? 6 Answers
How to play music/audio 2 Answers
SetActive on a different gameObject's component? Audio Source 1 Answer