Ryox92
1
Hey guys, I’m just wondering is it possible to randomize 2 or more clips from an AudioSource List by name? Something like this:
OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.CompareTag("Coin")
{
for (int i = 0; i < soundCtrlList.Count; i++)
{
if ((soundCtrlList_.clip.name == "Sound1") || (soundCtrlList*.clip.name == "Sound2"))*_
{
// Here I’m trying to see if I can play a random clip by name either Sound1 or Sound2
}
}
}
}
Of course, both SFX will be found by name since they are in the List. However, I’m just wondering if there is a way to randomly play one of 2 or more SFX by name.
Thanks
you use another list and use a loop inside a loop to check. if you did i this way you could do as many random sounds as you like
public string[] possibles = new string[]{"sound1","sound2"};
void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.CompareTag("Coin"))
{ List<string> s = new List<string>();
for (int i = 0; i < soundCtrlList.Count; i++)
{ for (int i2 = 0; i2 < possibles.Length; i2++){
if (soundCtrlList*.clip.name == possibles[i2])*
_ s.Add(possibles*);_
_ }}_
_ }_
_ int i3 = Random.Range(0,s.Count);_
_ print ("play: "+s[i3]);_
_ }*_
* }*