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 LordZephyr · Dec 31, 2014 at 06:10 PM · javascriptaudiorandomsound

Why is my random sound java script not working?

Hi, I am trying to create a button that, when you press it, you get a beep and then the same beep when you release your touch AND THEN one second after the second beep, there will be a randomly chosen sound from an array of 50 sounds that could be played. I studied a similar problem from http://answers.unity3d.com/questions/161417/random-audio-clip-help.html and put all my sounds in the array. I get two errors. One is: Assets/MyScripts/MaleSoberSound.js(31,18): BCE0044: expecting (, found 'PlayRandom'.

and the other is: Assets/MyScripts/MaleSoberSound.js(31,31): UCE0001: ';' expected. Insert a semicolon at the end.

I don't know where to search to find what I'm doing wrong in my script. I would greatly appreciate help on this javascript I created. Thanks so much in advance!!! Tom #pragma strict

 var sounds: AudioClip[];
 
 
 function Start ()
 {
 
 }
 
 function Update ()
 {
 
 }
 
 function OnMouseDown ()
 {
     audio.Play();
     //Application.OpenURL ("http://www.google.com");
     Debug.Log ("I touched the Microphone!");
 }
 
 
 function OnMouseUp ()
 {
 
     audio.Play();
     
     yield WaitForSeconds (1);
 
     function PlayRandom () //call this function to play a random sound.
     {
         if (audio.isPlaying) return; // don't play a new sound while the last sound hasn't finished.
         audio.clip = sounds[Random.Range(0,sounds.length)];
         audio.Play();
     }
     Debug.Log ("I released the Mic button.");
 
 }
 
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
Best Answer

Answer by LordZephyr · Dec 31, 2014 at 08:18 PM

Thank you so much #jimmycrazyskills!!! Without your help, I would not have been able to figure it out. The working script is below. Thanks again!!! Tom

 #pragma strict
 
 var sounds: AudioClip[];
 var bip: AudioClip;
 
 function Start ()
 {
 
 }
 
 function Update ()
 {
 
 }
 
 function OnMouseDown ()
 {
     audio.clip = bip;
     audio.Play();
     Debug.Log ("I touched the Microphone!");
 }
 
 
 function OnMouseUp ()
 {
     audio.clip = bip;
     audio.Play();
     
     yield WaitForSeconds (1);
 
     if (audio.isPlaying) return; // don't play a new sound while the last sound hasn't finished.
     audio.clip = sounds[Random.Range(0,sounds.length)];
     audio.Play();
 
     Debug.Log ("I released the Mic button.");
 
 }
 
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 jimmycrazyskills · Dec 31, 2014 at 08:35 PM 0
Share

np mate , dont suppose you know anything about multiplayer servers?

avatar image
1

Answer by jimmycrazyskills · Dec 31, 2014 at 06:23 PM

Hi mate you have your PlayRandom function inside your OnMouseUp function , I don't think you can do that I think it should help , sorry if it doesn't ;) below is what I think it should be like.

  function OnMouseUp ()
  {
      audio.Play();
      
      yield WaitForSeconds (1);
  
      function PlayRandom (); //call this function to play a random sound.
      Debug.Log ("I released the Mic button.");
  
  }
 
 function PlayRandom () //call this function to play a random sound.
      {
          if (audio.isPlaying) return; // don't play a new sound while the last sound hasn't finished.
          audio.clip = sounds[Random.Range(0,sounds.length)];
          audio.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 gryph · Dec 31, 2014 at 07:10 PM 0
Share

can't upvote yet, but looks like this should help a lot. +1

avatar image LordZephyr · Dec 31, 2014 at 07:30 PM 0
Share

Hi, #jimmycrazyskills and thanks! I tried what you said and it kind of worked. I took the function PlayRandom out of the mouseup and nothing happened. BUT, I took away the "function PlayRandom ()" and it worked but only once. Each time I press the button after the first time, I continually get the same sound and no more beeps. Here is the updated script. Any suggestions on how I can get this script to reset itself each time? Thanks so much for your help... I'm getting closer to the answer! $$anonymous$$

 #pragma strict
 
 var sounds: AudioClip[];
 
 
 function Start ()
 {
 
 }
 
 function Update ()
 {
 
 }
 
 function On$$anonymous$$ouseDown ()
 {
     audio.Play();
     Debug.Log ("I touched the $$anonymous$$icrophone!");
 }
 
 
 function On$$anonymous$$ouseUp ()
 {
 
     audio.Play();
     
     yield WaitForSeconds (1);
 
     if (audio.isPlaying) return; // don't play a new sound while the last sound hasn't finished.
     audio.clip = sounds[Random.Range(0,sounds.length)];
     audio.Play();
 
     Debug.Log ("I released the $$anonymous$$ic button.");
 
 }

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

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

Sound playing at random. (JS) 2 Answers

play sound when moving stop sound when not 1 Answer

Random footsteps 5 Answers

Need Audio To Stop When I Touch a Game Object. 1 Answer

Random Audioclip. No Repeat ?? 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