Trigger detects player collision and play specific audio from array
I want to have an object detect when a player collides with it and play a specific audio piece from an array. I got the trigger to play random audio from the array on startup, but i want to have a specific audio play from the array when the player collides with it.
UPDATE: I figured out why my player didn't collide with the trigger: I stupidly didn't capitalize the "o" on "OnTriggerEnter". HOWEVER I still have not found out how to select 1 specific audio piece to play from an array.
Here is my updated code:
 using UnityEngine;
 using System.Collections;
 
 public class AudioEventTriggerPlay : MonoBehaviour {
 
     public int PlayAudioValue;
     public bool PlayOnlyOnce;
     //array of audio i want to have play
     public AudioClip[] AvailableAudioCanPlay;
     AudioSource PlayAudio;
 
     // Use this for initialization
     void Start () {
         PlayAudio = GetComponent<AudioSource>();
     }
     
     //check if player collides with this object
     void OnTriggerEnter(Collider Other){
         if (Other.gameObject.name == "Player"){
             //debug check if player collided with the trigger
             print("The player touched me! D:");
             switch (PlayAudioValue){
                 case 0:
                     Debug.Log("Announcer tutorial string 1");
                     KillAfterPlay();
                     break;
                 case 1:
                     Debug.Log("Announcer tutorial string 2");
                     KillAfterPlay();
                     break;
                 case 2:
                     Debug.Log("Announcer tutorial string 3");
                     KillAfterPlay();
                     break;
                 case 3:
                     Debug.Log("Announcer tutorial string 4");
                     KillAfterPlay();
                     break;
                 case 4:
                     Debug.Log("Unreserved slot");
                     KillAfterPlay();
                     break;
                 case 5:
                     Debug.Log("Unreserved slot");
                     KillAfterPlay();
                     break;
                 case 6:
                     Debug.Log("Unreserved slot");
                     KillAfterPlay();
                     break;
                 case 7:
                     Debug.Log("Unreserved slot");
                     KillAfterPlay();
                     break;
                 case 8:
                     Debug.Log("Unreserved slot");
                     KillAfterPlay();
                     break;
                 default:
                     Debug.Log("Error: No available cases left or value is out of range!");
                     break;
             }
             //play audio from array Element 0
             /*if (PlayAudioValue == 0){
                 PlayAudio.clip = AvailableAudioCanPlay[Random.Range(0, AvailableAudioCanPlay.Length)];
                 PlayAudio.Play();
                 KillAfterPlay();
             }
             //play audio from array Element 1
             if (PlayAudioValue == 1){
                 PlayAudio.clip = AvailableAudioCanPlay[Random.Range(0, AvailableAudioCanPlay.Length)];
                 PlayAudio.Play();
                 KillAfterPlay();
             }
             //play audio from array Element 2
             if (PlayAudioValue == 2){
                 PlayAudio.clip = AvailableAudioCanPlay[Random.Range(0, AvailableAudioCanPlay.Length)];
                 PlayAudio.Play();
                 KillAfterPlay();
             }
             //play audio from array Element 3
             if (PlayAudioValue == 3){
                 PlayAudio.clip = AvailableAudioCanPlay[Random.Range(0, AvailableAudioCanPlay.Length)];
                 PlayAudio.Play();
                 KillAfterPlay();
             }*/
         }
     }
 
     // when bool PlayOnlyOnce is checked
     void KillAfterPlay(){
         if (PlayOnlyOnce == true){
             //destroys self when audio is finished playing
             Destroy(gameObject, AvailableAudioCanPlay.Length);
         }
     }
 }
Answer by pako · Jul 20, 2016 at 01:44 PM
Instead of using the Random.Range statement, you could use the index of the array in each case statement. For example:
              switch (PlayAudioValue){
                  case 0:
                      PlayClip(0);
                      KillAfterPlay(0);
                      break;
                  case 1:
                      PlayClip(1);
                      KillAfterPlay(1);
                      break;
                  case 2:
                      PlayClip(2);
                      KillAfterPlay(2);
                      break;
                  case 3:
                      Debug.Log("Announcer tutorial string 4");
                      KillAfterPlay();
                      break;
 
 etc.
              }
     private void PlayClip(int arrayIndex)
     {
         PlayAudio.clip = AvailableAudioCanPlay[arrayIndex];
         PlayAudio.Play();
     }
Also in KillAfterPlay() you must use the time of the clip in the Destroy statement, not the AvailableAudioCanPlay array length. So, include an index parameter.
     void KillAfterPlay(int arrayIndex)
     {
         if (PlayOnlyOnce == true)
         {
             //destroys self when audio is finished playing
             Destroy(gameObject, AvailableAudioCanPlay[arrayIndex].length);
         }
     }
Selecting the specific audio from the array seems to work. However when you mention the $$anonymous$$illAfterPlay index parameter, i get a compile error because there is a missing ")" in this line that you posted:
 //destroys self when audio is finished playing
              Destroy(gameObject, AvailableAudioCanPlay[arrayIndex].length;
After placing it myself, i still get a compile error:
There is no argument given that corresponds to the required formal parameter 'arrayIndex' of 'AudioEventTriggerPlay.$$anonymous$$illAfterPlay(int)'
Hi @jtgvhbth. Sorry for missing the closing ")". I typed directly in the answer form, rather than in Visual Studio or $$anonymous$$onoDevelop, so I didn't get a warning about it.
The error you are getting seems to indicate that somewhere in your code you are calling $$anonymous$$illAfterPlay() without a parameter.
By taking another look at my example above, I spotted another error, beside the missing ")". In "case 3" I just copied the previous code in line 22 $$anonymous$$illAfterPlay(); I wasn't paying much attention to such detail, because I was trying to explain a concept to you, thinking that once you understand the concept you would be able to fix these little details yourself.
I guess you are not yet familiar with interpreting compiler errors, but it's something that will become easier with experience.
So, make sure that all calls to $$anonymous$$illAfterPlay() will be with an integer parameter, e.g. $$anonymous$$illAfterPlay(3) in line 22, etc for other calls.
Hope this helps you.
You're right, i'm not yet familiar with compile errors, i'm still pretty new to c#, but i wish that to be my main language to code in.
As far as everything working: it does! Thanks so much for your help!
Your answer
 
 
             Follow this Question
Related Questions
Making a Game Save hitting a trigger 1 Answer
Can't figure out how to play a random audio clip when player collides with enemy 1 Answer
How do you trigger an audio clip when two game objects collide with each other? 0 Answers
How do you make audio play after GameObject Collides with another Game Object? PLZ REPLY QUICK 0 Answers
OnTriggerEnter no longer works 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                