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 kilian277 · Jul 13, 2012 at 08:42 PM · randomweather

Randomly choosing sounds ?

Hi,

i have a weather system in my game and currently only one sound for the thunderstorm.

But i wan't to change this to 3 sounds and those are randomly chosen so it's not like this :

1 -->2 ---> 3 and go back to 1 and start over it's more like this :

1-->3-->1-->2 and so on

So how do i do this ?

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
1

Answer by whydoidoit · Jul 13, 2012 at 08:55 PM

Put you audio clips in an array and then use Random.Range(0, clipArray.Length) to select an index - then play that one.

Comment
Add comment · Show 4 · 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 kilian277 · Jul 13, 2012 at 09:23 PM 0
Share

About the array how would i define an audioclip array because everytime i try to do that it gives me errors.

avatar image whydoidoit · Jul 13, 2012 at 09:27 PM 0
Share
    var clips : AudioClip[];
avatar image kilian277 · Jul 13, 2012 at 09:27 PM 0
Share

never$$anonymous$$d i searched google and i found it apperantly i was correct at the beginning ;D

avatar image kilian277 · Jul 14, 2012 at 08:46 AM 0
Share

sorry did not see that you already answered it

avatar image
0

Answer by Chimera3D · Jul 13, 2012 at 09:07 PM

Well first of all if you have only one sound you can't generate two other different sounds. If you do have three sounds then you can create an array and choose randomly which sound you want. To do this simply use Random.Range. You can say.

 var sounds : AudioClip[];
 var tomeBetweenThunderBolts : float;
 
 private var timer : float;
 
 function Update (){
 
      var randomSound = Random.Range(1, 3);
 
      timer += (Time.deltaTime);
 
      if (timer >= timeBetweenThunderBolts){
 
           gameObject.GetComponent(audioSource).clip = sounds[randomSound];
 
           audio.Play;
 
            timer = 0;
 
       }
 
 }

I didn't test it so if there's a bug let me know and I'll fix it. What you are going to do is you are going to put all of your sounds in the sounds array, then you have to set the timeBetweenThunderBolts (I couldn't think of another variable name) then you're done!

Comment
Add comment · Show 6 · 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 kilian277 · Jul 13, 2012 at 09:23 PM 0
Share

i get an error with using parts of your script : Type 'UnityEngine.AudioClip' does not support slicing.

avatar image kilian277 · Jul 13, 2012 at 09:28 PM 0
Share

Sorry, did something wrong no errors now, but i'm no going for a good night time sleep so i will try in the morning !

avatar image Chimera3D · Jul 13, 2012 at 10:03 PM 0
Share

I forgot to make the sounds variable an array, sorry about that. I updated the script.

avatar image kilian277 · Jul 14, 2012 at 08:45 AM 0
Share

yes i now i changed it after is searched google, i'm now going to implement this

avatar image kilian277 · Jul 14, 2012 at 09:09 AM 0
Share

It works now ! , thanks everyone fdor the help!

oops wrong edit box this should be a comment

Show more comments

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Randomized weather, little stuck 1 Answer

random prefab instantiation 2 Answers

Simple array an spawning question 6 Answers

Make an object move towards random spot on another objects edge? 1 Answer

Puzzle + Grid Instantiate - Random 2 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