I can attach multiple audio sources to a single game object, but can’t seem to play them. I’m not trying to play them all at once. I just want to play them in sequence, one right after the other, without having to swap the clips in between. I want to attach them to the same object because they’re 3D sounds that play while it’s moving around the scene.
I couldn’t figure out a way to select a particular audio source in the inspector, so I did it in code:
tCom = incidentBallTrackTrans.gameObject.GetComponents(AudioSource);
for (i = 0; i < tCom.length; i++) {
if (tCom[i].clip.name == "steel roll track")
incidentBallTrackSnd = tCom[i];
if (tCom[i].clip.name == "steel hits floor")
incidentHitsFloorSnd = tCom[i];
if (tCom[i].clip.name == "steel roll floor")
incidentRollSnd = tCom[i];
}
There are three audio sources attached to the one game object, and I assign each one to a different AudioSource variable. As far as I can tell, the correct audio source gets assigned to the correct variable, but they all play the same sound. It’s the sound that is listed first in the inspector.
All the variables have the correct audio source assigned, but they all play the same clip, the one attached to incidentBallTrackSnd. How can I play the different clips assigned to the different sound sources? Or is that not possible?
One other thing I just discovered, if I get rid of .name and just do .clip == “name”, the first two audio sources play correctly, and the third works as before (plays the first clip).
I think the “Unity way” to do this would be to keep a list of audio clips and swap them. Any reason you can’t do that?
If the wrong sounds are getting assigned to your variables then your conditionals are not evaluating like you assume the are. AFAIK, the “==” operator in UnityScript is overloaded for string comparison so you should be ok there. I would check what the name property is actually returning and go from there.
A general note: You should string together your conditionals with “if else” clauses so you don’t have to evaluate each one every time.
I think most probably John wanted to get multiples 3D sound play together.
I’ve not try attached multiple audio source in 1 (one) GameObject,
but I can tell that multiple GameObjects that attach 1 (one) audio sources each, then assign all of them into one parent Works.
I haven’t tried using multiple audiosources via the inspector, just in code.
Here is pseudocode
Declare your arrays:
public AudioClip[] Clips;
private AudioSource[] audioSources;
Assign the sounds to your clips array in inspector…
Then add the audiosource components to the object, and assign the clips to the corresponding audiosource.
public void Start()
{
audioSources = new AudioSource[Clips.length];
int i = 0;
while (i < Clips.length)
{
GameObject child = new GameObject("Player");
child.transform.parent = gameObject.transform;
audioSources[i] = child.AddComponent("AudioSource") as AudioSource;
audioSources[i].clip = Clips[i];
i++;
}
}
Then you can play the clips like
audioSources[0].Play();
audioSources[1].Play();
You could use enumerators in place of the index numbers if you want to be more clear which audiosource is which sound.
I don’t want to play the sounds simultaneously. But I do want to play them very close together, the second almost immediately after the first, without the time required to swap clips between the end of one sound and the beginning of the next.
You can use PlayScheduled to schedule the sounds to play one after the other.
It takes a time value as an argument. You can query the current time via AudioSettings.dspTime
Then all you have to do is PlayScheduled the sounds to play one after the other by adding their lengths in seconds to the current time. So something like this
double Now = AudioSettings.dspTime;
float InSeconds = 0.0f;
int i = 0;
while (i < Clips.length)
{
audioSources[i].PlayScheduled(Now + InSeconds)
InSeconds += audioSources[i].clip.length;
}