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 /
avatar image
0
Question by kab_6182 · Jun 17, 2017 at 05:56 AM · audiosourceactivationrandomizing

Randomly Play an audio Source

I have multiple audio sources through out my scene. I am working on a 3d audio project. Upon entering play mode I want the audio sources to be triggered randomly one after another without repeating before all sounds are played. Just to make it clear I dont want random audio clips, which I could put in an array and loop through. I want the audio sources to be triggerd from their respective position as I am working on audio spatialization.

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 HawkSandwich · Jun 17, 2017 at 07:21 AM

Heya :)

The way I'm going to do this is by having an array of the AudioSources in your scene. Each audio source will of course have its own position, and you should set their audio clips in the inspector. Next, we're going to fill an empty list with all the AudioSources in the array. The next step is to trigger a random AudioSource from that list, remove it from the list, and repeat until the list is empty. Then we start the whole cycle over again. For simplicity, I'll have it play a sound every second. Of course you could get the audio clip's length and wait that amount of time if you want. Also, don't forget to put "using System.Collections.Generic;" in your script.

 public AudioSource[] sourcesInScene; //assign these in the inspector
 List <AudioSource> sourcesList;
 void Start () {
     StartCoroutine (SoundsTick());
 }
 IEnumerator SoundsTick () { //we're using an ienumerator so we can "wait" between loops.
     while (true) {
         sourcesList = new List<AudioSource>();
         for (int x = 0; x < sourcesInScene.length; x ++) {
             sourcesList.Add(sourcesInScene[x]);
         }
         for (int z = 0; z < sourcesList.count; z ++) {
             yield return new WaitForSeconds(1f);

             int i = Random.Range(0, sourcesList.count);
             sourcesList[i].Play();
             sourcesList.Remove(i);
         }
     }
 }


There might be a few errors in this code, so instead of copying it directly, maybe just use it as a guide instead. Hope I helped :)

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 kab_6182 · Jun 18, 2017 at 08:08 PM 0
Share

you are the best the code is awesome, just a little bit of hiccup the random loop sometimes pick the same before going through all the sources meaning the remove method is not removing the the sources.

avatar image JxWolfe kab_6182 · Jun 18, 2017 at 08:11 PM 0
Share

if you like the job then you might want to set the answer as the "best answer" and help sandwich...

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

70 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

Related Questions

Account Activation 0 Answers

Manual/Offline Activation of Unity 3.x 12 Answers

Unity won't open, or go past blank license screen 0 Answers

is it Possible to Move a UI Slider remotely by script? If So How 1 Answer

wav "clicks" on audio.Stop() and audio.Play() on iPhone 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