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
0
Question by DimitriUK · Jun 25, 2014 at 12:38 PM · inputarrayaudiomultiple

Playing multiple sounds on same key input ("e")

Hi people,

So, I haven't been on Unity Answers in a while as I like to learn things the hard way.. However, this one has just completely frustrated me and I just can't seem to wrap my head around it.

The way I want this to work is by the character going up to a cup to press the E key which should then play the first sound, WAIT FOR IT TO FINISH, then when they press the E key again, it should play the soundClip2 AudioClip and then WAIT FOR IT TO FINISH, and lastly, play the soundClip3 when the user presses E.

After that is all done, I want it to go back to the top of the script. Do I need to add a Function Update or anything?

It would be really appreciated if you could get me sorted on this problem as it's begun to put a halt on my project.

public var soundClip1 : AudioClip; public var soundClip2 : AudioClip; public var soundClip3 : AudioClip; // Play default sound function OnTriggerStay (col:Collider){ if(Input.GetKey("e")) { audio.clip = soundClip1; audio.Play(); yield WaitForSeconds (soundClip1.audio.clip.length); } else if(Input.GetKey("e")) { audio.clip = soundClip2; audio.Play(); yield WaitForSeconds (soundClip2.audio.clip.length); } else if(Input.GetKey("e")) { audio.clip = soundClip3; audio.Play(); yield WaitForSeconds (soundClip3.audio.clip.length); } }

Here is the scene I am working on

alt text

Because I can't get this script to work, I have decided to stick to a shortenered version of it, but that's not what I want as it only plays one sound. ALSO when I keep pressing E, it spams the sound.. I want it to play the sound once, and then when it finishes, it plays again when the E key is pressed. I have tried PlayOneShot, and it tells me something like Unity can't support it or so..

 public var soundClip1 : AudioClip;
 public var soundClip2 : AudioClip;
 public var soundClip3 : AudioClip;
     // Play default sound
        
    function OnTriggerStay (col:Collider){
     
 
         if(Input.GetKey("e")) {
              audio.clip = soundClip1;
              audio.Play();
              yield WaitForSeconds (soundClip1.audio.clip.length);     
            
      }
     }
   

**If you manage to understand the problem I am facing, I won't know how much to thank you! Please please please! Help me out! The Unity Community is very good and that is why I have chosen to write my issues here.

Thanks.**

press e on cup.jpg (146.7 kB)
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
1
Best Answer

Answer by Nerevar · Jun 25, 2014 at 01:06 PM

Hello,

Assuming the OnTriggerStay() is actually triggered, I think you can simply do like this:

 public var soundClip : AudioClip[]; //to be assigned in inspector
 private var looper : int = 0;
 
     function OnTriggerStay (col:Collider){
      
          if(Input.GetKey("e")) {
              if(!audio.isPlaying)
              {
                      audio.clip = soundClip[looper];
                      audio.Play();
                      looper++;
                      looper = looper%soundClip.Length;
             }    
      
          }
      }


regards

Comment
Add comment · Show 1 · 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 DimitriUK · Jun 25, 2014 at 09:38 PM 0
Share

Hi Nerevar,

This is absouletly brilliant mate! This was exactly what I was looking for. Thank you so much! I've also learned from this.

Brilliant stuff!

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

Play sound upon Input in Javascript 1 Answer

How to use Multi-Tap in New Input System for Running? 1 Answer

[URGENT] WebGL export completely different from editor playtesting. [C#] 1 Answer

Is this doable in unity 2 Answers

Why is the keypress only registered on the second press? 3 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