• 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 Kupo_01 · Dec 10, 2014 at 04:55 AM · audiosoundmanagerclip

Making sounds wait for each other to finish

I'm trying to set up a script in which a sound begins to play at a certain point in time, which, in this case, is when the screen stops fading from black. I have a separate script that does that, and a public boolean that sets itself to true once the alpha of the image is less than .05. In my other script, the one I'm executing the sound from, I set up my sound manager as a public variable, and use it to play sounds in code. I have two sounds I would like to play in a sequence. However, I can't figure out how to get the second sound to play only after the first one has finished, and with my current code, both sounds alternate with each frame.

Here's my code so far: http://pastebin.com/nBfpbQ46

Now, I'd prefer to just be able to define audio clips as variables and play them that way, but I can't figure out how to get that to work.

Any suggestions?

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
0

Answer by Mapleman · Dec 10, 2014 at 06:57 AM

If you always want to play those sounds in sequence, wouldn't it be easiest just to combine them in one clip?

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
0

Answer by ajk48n · Dec 10, 2014 at 06:57 AM

You can do this by making use of a coroutine. That's a function that you can delay before it starts. So you would do something like the following.

 float lengthOfOriginalSound = sound.clip.length;
 StartCoroutine(delayedPlay(lengthOfOriginalSound));
 
 
 IEnumerator delayedPlay (float timeToWait)
 {
    yield return new WaitForSeconds(timeToWait);
 
    // play second sound
 }


In order to delay a function call, the function must have a return type of IEnumerator. When a function has this you can call the yield line above, which will pause execution of that function for the length of time you pass to it. Then after the yield has finished, you can play your new sound.

In order to call a function, you must call it with StartCoroutine() from your original function.

Also, this code is specific for C#. The syntax is a bit different if you are using Javascript.

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

26 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

Related Questions

Audio Play Once 2 Answers

Synch clips with BPM 2 Answers

How To Turn Off Sound For Certain Objects? 1 Answer

Can you make this work? (Click Collider = Make Sound) 2 Answers

Audio Scripting 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