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 SilverHorrorDev · Sep 07, 2013 at 01:05 PM · sound

Is it possible to make a random sound script?

I'm looking for a way to make a script that waits for a few seconds then plays a random noise (I would select the noises it would be able to choose in the script) then waits again and chooses a random noise again and again until the scene has been finished. Is this possible and if so what functions should I be using? Sorry if this is asking too much. Thanks in advance.

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
2
Best Answer

Answer by clunk47 · Sep 07, 2013 at 05:00 PM

Here's a more simplified example. You assign each clip you want in your array using the inspector. Then of course you need to place your own conditions to make the boolean true. This gives clip index a random range between 0 (first index), and clips.Length - 1 (last index) of the array. Also be sure to check that the audio is not playing before you try to play it.

 #pragma strict
 
 var clips : AudioClip[];
 var clipIndex : int;
 var playAudio : boolean = false;
 
 @script RequireComponent(AudioSource);
 
 function Update()
 {
     PlaySound();
     
 }
 
 function PlaySound()
 {
     yield WaitForSeconds(Random.Range(1, 10));
     if(playAudio && !audio.isPlaying)
     {
         clipIndex = Random.Range(0, clips.Length - 1);
         audio.PlayOneShot(clips[clipIndex], 1.0);
     }
     playAudio = !playAudio;
 }
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 SilverHorrorDev · Sep 07, 2013 at 05:33 PM 0
Share

Thanks very much for this. Could you explain a bit more on how to use this script? I'm really new to coding, so sorry if I'm being a bit stupid about this.

avatar image MountDoomTeam · Sep 07, 2013 at 05:52 PM 1
Share

copy the script onto an object, then you will be able to see it's variables in the Inspector, you will see it has an audio array, it's an array of sounds, fill the array with sounds, for example 5 sounds, and then play unity. If you want to learn how to make a game at all, you will have to get into the habit of playing with scripts for one hour to discover what they do and to learn about them by yourself, one hour of study will $$anonymous$$ch you more than any amount of questions you could ask people here.

I just spent 3 days learning shader language, all you do is take a script, change some of the numbers, copy lines from similar scripts, research what the words mean on unity reference, and soon you will actually be a game developer.

avatar image SilverHorrorDev · Sep 07, 2013 at 05:56 PM 1
Share

Thanks very much, for the help and the advice. :)

avatar image clunk47 · Sep 07, 2013 at 06:57 PM 1
Share

$$anonymous$$any tutorials, including basics of how to use the interface http://unity3d.com/learn/tutorials/modules

avatar image rjeike · Jun 19, 2016 at 12:18 AM 1
Share

There is a bug in the script, it will never play the last AudioClip in the array. The second parameter (max) to Random.Range is exclusive (i.e. it should be just clips.Length).

Show more comments
avatar image
1

Answer by mirkobon · Sep 07, 2013 at 01:58 PM

yes it is here is the script i tested:

 #pragma strict
 
 // add as many AudioSources as you want
 var Audio1 : AudioSource;
 var Audio2 : AudioSource;
 var Audio3 : AudioSource;
 var Audio4 : AudioSource;
 var PlayAudio : int;
 
 function Start () {
 
 }
 
 function Update () {
 // Dont forget to change the max number if you incres the amount of audio sources
 PlayAudio = Random.Range(1, 4);
 Playsound();
 }
 
 function Playsound()
 {
 yield WaitForSeconds (5);
 {
 // if you added audiosources make sure to add them in the play sound
 if (PlayAudio == 1)
 {
     Audio1.Play();
 }
 if (PlayAudio == 2)
 {
     Audio2.Play();
 }
 if (PlayAudio == 3)
 {
     Audio3.Play();
 }
 if (PlayAudio == 4)
 {
     Audio4.Play();
 }
 }
 }
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 mirkobon · Sep 07, 2013 at 02:11 PM 0
Share

if this helped you please mark it as accepted

avatar image SilverHorrorDev · Sep 07, 2013 at 02:36 PM 0
Share

Thank you very much for the script, but there is one problem. I get this error when trying to use it: Assets/Standard Assets/Script/Random Ambience.js(25,1): BCE0043: Unexpected token: if.

Could you help me with this?

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

20 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

Related Questions

Walking sound Script returns with Error: BCE0077 2 Answers

Play a sound when the wind blows? 0 Answers

I want my trigger sound only to play once! 0 Answers

Sound help with script 1 Answer

Sound on collisions 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