- Home /
 
               Question by 
               Ryox92 · Jun 26, 2019 at 01:05 AM · 
                listaudiosourcerandom.rangerandomize  
              
 
              Randomize 2 clips from an AudioSource List by name?
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[i].clip.name == "Sound1") || (soundCtrlList[i].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
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by toddisarockstar · Jun 26, 2019 at 03:23 AM
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[i].clip.name == possibles[i2]) 
                 {
                         s.Add(possibles[i]);
                     }}
             }
             int i3 = Random.Range(0,s.Count);
             print ("play: "+s[i3]);
         }
         
         }
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                