C# Multiple audioSource with the same script and same sound accessed by one script

Hi,

As I explained in the title, I’ve got 20 audiosources with 2 sounds each, there are 20 buttons when you press the good one it plays the good sound, if you press the wrong > wrong sound.

I’ve tried with only one audio source and the problem is when you press quickly the good buttons in a row well I want the sounds to overlay. So I thought of using multiple audiosources.

I’ve got one script to manage the audiosource:

public class SoundPlayer : MonoBehaviour {
	
	public static bool hit;
	public static bool hitfail;	
	public AudioClip Hit;
	public AudioClip HitFail;	
	
	// Use this for initialization
	void Start () {
		hit = false;
		hitfail = false;
	}
	
	// Update is called once per frame
	void Update () {
		
		if(hit == true)
		{
			audio.clip = Hit;
			audio.Play();
			hit = false;
		}
		if(hitfail == true)
		{
			audio.clip = HitFail;
			audio.Play();
			hitfail = false;
		}
	
	}
}

Yep pretty simple…

In general, in c#, when I need to access a variable from a script to another, i’m using this:

OtherScriptName.NameOfMyVar = …

So atm, in my otherscript i’m doing this: if button pressed = good → SoundPlayer.hit = true; else SoundPlayer.hitfail = true;

Obviously with 20 audiosources with the same script it will play the sound in all the audio sources at the same time.
How can I access to the SoundPlayer.cs of one particular gameobject? Using Arrays, because I already detect the button with array and a for loop so is there something like AudioSourceGameObject*.SoundPlayer.hit = true; ?*
I’m working on iPad so I need it as efficient as it can be (it’s my first ipad game so still learning ^^ ), I don’t mind if I need to duplicate the script, that’s how I did it in the past but it was a matter of having two differents scripts not 20…
Thanks for the advice !!

bump :smile:

Take a look at my most recent post in the other one about audioplay, it should work just for you with a little adapt.

Hi Dabeh,

Thanks a lot for your answer!

I read your post too fast and I started to look at your infinite loop one. I came up with an idea and I would like to know what’s doable and if it’s not too performances consuming.

Everytime you press a pad I could, create and instance of my audioPlayer prefab and destroy it after playing. The whole idea of doing 20 of them is to be able to play the same sound (or different ones) all almost at the same time, if you have only one audio source when you press a new button it stops the previous sound and plays the new one. By using 20 audiosource for 20 buttons I would still be limited to play the sound for pressing the same button twice in a row.

So my idea is to do something like the seagulls sound system in the island demo, press a button > create an audio source which plays his sound then destroy itself. Like that if I press really fast one button well the sound will be fully played as many time as i pressed the button.

No sure if it’s really good, but that’s what I came up with.

I obviously read back your post and read you post about the audioplay and it is what i was looking for thanks :wink:

Easily doable, probably use a List or something for that.
But whats this:
I would still be limited to play the sound for pressing the same button twice in a row.

if you use two buttons, which triggers one audiosource with script above. If you press the first one and then quickly the second one, well it plays the first sound and as soon as you press the second button it stops the button and play again the sound from start. The same sound because it’s setup like that, but if you use two different music, it should stop the first one to play the second one, and as it’s sound effect I want the two sounds to co-exist. By using different audio sources I solved that issue, in the case of you press quickly different buttons in a row.

But if you press the same button twice, the same problem appears again because each button is linked to one only audio source.

That was to try to answer your question, I’m not sure it is clearer ><

I’m trying with this now (trying to make it work first then I’ll try the instantiate stuff):

if(button pressed)
{
if (button == good)
SoundPlayers*.GetComponent().hitgood();*
else
SoundPlayers*.GetComponent().hitbad();*
}
I had to create the function hitgood() and hitbad() which have just hit = true and hitfail = true because I can’t do that line:
SoundPlayers*.GetComponent().hit = true;*
Not sure why but that works now.
Here is the script:
```
*public class SoundPlayer : MonoBehaviour {

public static bool hit;
public static bool hitfail;


public AudioClip Hit;
public AudioClip HitFail;




// Use this for initialization
void Start () {
	hit = false;
	hitfail = false;
}

// Update is called once per frame
void Update () {
	
	if(hit == true)
	{
		audio.clip = Hit;
		audio.Play();
		hit = false;
	}
	if(hitfail == true)
	{
		audio.clip = HitFail;
		audio.Play();
		hitfail = false;
	}

}

public void hitgood()
{
	hit = true;
}
public void hitbad()
{
	hitfail = true;
}

}
_*_</em> <em>_*Here is the second script:*_</em> <em>_**_

*if(Physics.Raycast(ray, out hit))
{
for(int i = 0; i<Pads.Length; i++)
{
if(hit.collider == Pads[i].collider)
{
if(TouchOncePad[i] == true)
{
PadNum = i;
if(i == Combos[ComboNum][Pad]-1)
{
PadInCombo = true;
SoundPlayers[i].GetComponent().hitgood();
}
else
{
PadInCombo = false;
ComboOK = false;
SoundPlayers[i].GetComponent().hitbad();
}
StartCoroutine(“TouchProcessing”);
for(int a=0; a<TouchOncePad.Length; a++)
{
TouchOncePad[a] = true;
}
TouchOncePad[i] = false;
}
}

}

}
_```*
The problem is that it doesn’t seems to use the different SoundPlayers (the i is in a for loop) and it’s only the SoundPlayer which doesn’t work because i’m using the i the same loopand it works fine…
I’m trying with one sound per audiosource to see wich one is played all the time…_

Would love to help more but this is as far as I go in the sound department. Sound isn’t my thing :stuck_out_tongue:

Héhé, me too that’s the problem :smile:

Thanks anyway for your time and help Much appreciated!

Let’s open the documentation again…

Try using audio.PlayOneShot() instead of just play, this should allow you to have just one AudioSource. You really don’t want to have a separate source for every sound, and doing GetComponent every time there’s an event isn’t optimal.

Thanks a lot exactly what I was looking for, everything works with only one audiosource in the scene now. Perfect !