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 rogmidd · Dec 10, 2014 at 02:22 AM · c#coroutineaudioclip

How to select a random audioclip to be played in multiple places

I'm using a random array to select an audio clip, and need to send whichever clip is selected to multiple parts of my code simultaneously, including Coroutines. I'm using the Coroutines so i can use the yield return new WaitForSeconds command.

My problem is that, the way it's written now, the random sound will be selected separately in each piece of code, so both sources won't necessarily be playing the same thing. Is there a way i can trigger an audioclip to be randomly selected on GetButtonDown, which can then be somehow referenced by both the Coroutine and the GetButtonDown? I apologise in advance if this has an easy solution that i've overlooked, or has been answered elsewhere; i'm very new to C#/any code at all, and have been squinting and scratching my head over this for a few hours now.

The code looks something like this (i've stripped out anything that seemed irrelevant):

 public AudioClip[] SoundEffect;
 
 
 void Update () {
 
 if (Input.GetButtonDown ("Fire1")){
 
 AudioSource.PlayClipAtPoint(SoundEffect [Random.Range (0, SoundEffect.Length)], transform.position);
 
 StartCoroutine( PlayElsewhere() );
 
 }
 }
 private IEnumerator PlayElsewhere(){
 yield return new WaitForSeconds (5);    
 AudioSource.PlayClipAtPoint (SoundEffect [Random.Range (0, SoundEffect.Length)], transform.Elsewhere);//i know transform.elsewhere probably doesn't exist, it's just a stand-in 



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 Vandarthul · Dec 10, 2014 at 01:45 PM

 public AudioClip[] SoundEffect;
 private AudioClip randomlySelectedAudio; // Add this line.
  
  void Update () {
  
  if (Input.GetButtonDown ("Fire1")){
  randomlySelectedAudio = SoundEffect[Random.Range(0, SoundEffect.Length]; //Add this line
 
  AudioSource.PlayClipAtPoint(randomlySelectedAudio, transform.position);  //Change this line accordingly
  
  StartCoroutine( PlayElsewhere() );
  
  }
  }
  private IEnumerator PlayElsewhere(){
  yield return new WaitForSeconds (5);    
  AudioSource.PlayClipAtPoint (randomlySelectedAudio, transform.Elsewhere);//Change this line accordingly

I've modified your code for basically when Clicking to "Fire1" button, it will select a random AudioClip and play it, also it will play the same audio on StartCoroutine function. So I've added a variable that will hold the randomly selected audio clip whenever player clicks "Fire1" button and assigned it to the places where you want to play audio(in "Update" and "PlayElsewhere")

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 rogmidd · Dec 10, 2014 at 10:40 PM 0
Share

Looks like that did it, thank you! I was almost there at one point, but didn't have the private AudioClip randomlySelectedAudio line. Cheers!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Fade, Then Stop all other audiosources attached to gameobject? 1 Answer

How can I save and load a Coroutine state ? 0 Answers

StopCoroutine Does Nothing 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