Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by jtgvhbth · Jul 16, 2016 at 07:08 PM · collisionarrayaudiotriggerplayer

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);
         }
     }
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
         }
     }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jtgvhbth · Jul 20, 2016 at 04:36 PM 0
Share

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)'

avatar image pako · Jul 21, 2016 at 01:15 PM 0
Share

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.

avatar image jtgvhbth pako · Jul 22, 2016 at 06:09 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

93 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges