- Home /
Play audio clip on more than one object from another gameobject
Hey guys/girls.
I want to play a audio file on more than one gameObjects(with audioSource attached to them)
I am creating an empty object and attaching this script
public class AudioPlayScript : MonoBehaviour {
public GameObject[] obj;
public AudioClip song;
bool check = true;
void Start ()
{
check = true;
obj = GameObject.FindGameObjectsWithTag("Audio");
}
void Update ()
{
if (Input.GetKeyDown (KeyCode.P) && check == true)
{
foreach(object o in obj)
{
//For each Obj from the array will be called PlaySound
//function
//I need some help here.
}
check = false;
}
}
public void PlaySound (AudioClip sound)
{
audio.clip = sound;
audio.Play ();
}
public void PauseSound (AudioClip sound)
{
audio.clip = sound;
audio.Pause ();
}
}
The array will contain all those gameObjects spread into my scenes from where will be played my Only mp3 file.
The problem is that by playing more than one sound at the same time in my scene (same mp3 file) will give me an echo and I dont want it to be.
I need help of how to do this.
I don't follow, the problem implies you want to play a sound file simultaneously on multiple gameobjects, the code stub you have setup also does as well since it's preparing to iterate over an array, but your closing comment implies you don't want to play more than one sound at the same time. That's the part that threw me.
Have you looked at audio.PlayClipAtPoint if you are trying to play the sound at a particular gameObject's location?
Well, I have 10 objects and I want to play for everyObject the same mp3 file. But I get that echo effect.
That's why im iterating through an array.
I tried with PlayClipAtPoint but still I got the effect of echochness, and this effect is due to playing the same file multiple times on different locations, because it's overlapping each other.
I want to get rid of that echo effect.
I'm sure I misunderstand, because if you have 10 things playing the same sound file the slightest differeces between them will put them out of exact synch so you'll get your 'echo'
What do you need to play all those sounds at once for? It might be because your sound is set to 3D? if your audio listener isn't the same distance from each game object then you'll get an echo because Unity simulates the speed of sound which you can set in the audio manager. Try setting the sound to 2D in the import settings.
Imagine you have a big stage with a 10 speakers on it, How would you do to play the same song on each of them?
Answer by jabez · Apr 25, 2014 at 10:35 AM
Attach the script you have now to each gameobject you want sound to come from, then use AudioPlayScript.PlaySound(); you may have to change the PlaySound function in your script from public void to public static void, in another script put AudioPlayScript.PlaySound();, the only thing is you'll have to create a script for each sound. OR use this, i modified your script for you
using UnityEngine;
using System.Collections;
public class AudioPlayScript : MonoBehaviour {
public GameObject[] SoundSource;
public AudioClip song;
bool check = true;
void Start ()
{
for(int i = 0; i < SoundSource.Length; i++) {
SoundSource = GameObject.FindGameObjectsWithTag("Audio"); // You may have to select the SoundSources in the inspector
}
}
void Update ()
{
if (Input.GetKeyDown (KeyCode.P))
{
PlaySound(song);
}
}
public void PlaySound (AudioClip sound)
{
for(int i = 0; i < SoundSource.Length; i++) {
SoundSource[i].audio.clip = sound;
SoundSource[i].audio.Play();
}
}
public void PauseSound (AudioClip sound)
{
for(int i = 0; i < SoundSource.Length; i++) {
SoundSource[i].audio.clip = sound;
SoundSource[i].audio.Pause ();
}
}
}
You need to set the Doppler effect level to 0 for each audio source too or you get a weird chorusing effect when you move around.
add SoundSource[i].audio.dopplerLevel = 0.0f; to start function
For those with the doppler. I already had the doppler set to zero.
Doppler makes your audio wrong when you move, but even if I am not moving I got that effect of echo. This echo is due to multiple sounds played at once.
@jabez your script is perfect (I made something like this before), but $$anonymous$$y problem is not in doppler, my problem is in echo, and this echo effect is done by playing multiple sounds at the same time.
I still got it. :(
Hmm, You could just make it so the audio plays from the $$anonymous$$ain camera ins$$anonymous$$d of multiple objects but it wont be 3D sound.
Answer by jakeflaze3859 · Aug 17, 2014 at 03:17 PM
I'm not sure if this is what your looking for, but could you just reduce the max distance under 3D sound settings in the audio source component?
Your answer
Follow this Question
Related Questions
Audio Sounds Blurring Together 0 Answers
play audio from frequency and amplitude file 0 Answers
Find all Objects/Scripts that access a given audio!! 1 Answer
Truncate audio runtime 0 Answers
Intensity of sound. -1 Answers