Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
1
Question by Ouija · Jan 30, 2014 at 02:57 AM · audiomultiplekeypress

Multiple Sounds Clips when key pressed

If you can direct me to a previous post that would be great. I cannot find an answer for my question. So I will ask.

I am wondering how to make multiple audio clips play when you press a key. But only when near an object can they play. Example: you walk up to a cube, press enter it plays a sounds. Press enter again, it plays another one. I am also hoping to let the audio fully play out before the second audio can be played. I have asked a similar question before but it is for mouse click and to only play one clip. Check that out maybe!

Any idea will help!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Supercrusher9000 · Jan 30, 2014 at 03:48 AM

My guess would be to create an int variable simply by doing var [variable name] = 0. Then in the function, after it plays the sound just type Debug.Log([variable name]++); This will make it so when ever you press enter near the cube it switches to the next object. So simply have an if statement that says if([variable name] == 1){(then play that clip. So say you have 3 clips to play do that for all three and then have an if statement that states if([variable name > 3])(set [variable name to 0]) Sorry if this is hard to follow if you are still new, I don't have time to write out all the code. So let me know if your confused on anything.

Comment
Add comment · 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
1

Answer by Ashish Dwivedi · Jan 30, 2014 at 04:01 AM

Try with this class. Simply call "PlayAudio(clip)" method by passing audio clip. I think this would help you.

 using UnityEngine;
 using System.Collections;
 
 public class PlayAudio
 {
     public delegate void AudioCallback();
     
     public void PlaySound_newStyle( AudioClip clip)
     {
         if(isSoundoff)
             return;
         AudioSource audioSourceTemp = gameObject.AddComponent<AudioSource>();
         audioSourceTemp.PlayOneShot(clip);
         StartCoroutine(DelayedCallback(clip.length, ()=> Destroy(audioSourceTemp)));
     }
     
     private IEnumerator DelayedCallback(float time, AudioCallback callback)
     {
         yield return new WaitForSeconds(time);
         callback();
     }
 }


Comment
Add comment · Show 2 · 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 Ouija · Jan 30, 2014 at 10:56 PM 0
Share

Thanks for the replies! I will attempt this when I get home tonight. Any ideas how multiple audio would work? Basically I want it for a character, when you walk close to him and press enter, it will run through four different sounds clips each time you press enter. But only after the audio clip is finished playing, seems more natural for speaking audio :)

avatar image valim · Jan 31, 2014 at 09:29 AM 0
Share

For multiple sounds:

 public AudioClip[] clip;
 
 .
 .
 .
 
 AudioSource audioSourceTemp = gameObject.AddComponent<AudioSource>();
 audioSourceTemp.PlayOneShot(clip[Random.Range(0, clip.Length)]);

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

21 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

Related Questions

How can I play multiple audioclips from the same object? 2 Answers

Barrel Roll on Double Key Press, right & left 2 Answers

Multiple audioSources on same gameObject? 1 Answer

Problem with pressing multiple keys at once? 2 Answers

multiple sounds, sound....sucky 0 Answers


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