• 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 byurocks23 · May 09, 2014 at 04:35 AM · javascriptaudioloopscore

Have readout of score at certain increments (JavaScript)

I am making a game where the score is incremented gradually and not incrementally. The score goes up depending on how long you live, so it is constantly going up the whole time. The new score gets refreshed every second and when I print the score to the console I get numbers such as, 0.05037879, 0.07613637, 0.1018939, etc.

I have recorded voices saying a single number, and by combining multiple sound files, i can get a readout of the number I want. I want when the score reaches 0.2, for it to read out "0.2". I then when it reaches 0.5, it should readout "0.5". Then when it reaches 1.0 it read out "1.0". From then on it should loop and readout the correct number at every increment of 0.5 (1.5,2.0,2.5 etc). Technically t$$anonymous$$s should go on forever, but no one will reach to a score of 10, so the last number will be 9.5. (so all of the read out numbers are 0.2, 0.5, 1.0, 1.5, 2.0, 2.5 .......... 8.5, 9.0, 9.5) what I have, as far as I can tell, should work. Here is what it is,

............................................................................................

 var zero: AudioClip;
 var one: AudioClip;
 var two: AudioClip;
 var three: AudioClip;
 var four: AudioClip;
 var five: AudioClip;
 var six: AudioClip;
 var seven: AudioClip;
 var eight: AudioClip;
 var nine: AudioClip;
 var point: AudioClip;
 var miles: AudioClip;
 var youTraveled: AudioClip;
 var score1: int;
 var firstNum: int;
 var secondNum: int;
 var t$$anonymous$$rdNum: int; 
 
 var pointTwo = true;
 var pointFive = true;
 var pointZero = false;
 
 function Update(){
     score1 = Mathf.Round(Score.distanceScore * 100);
     firstNum = score1/100;
     secondNum = (score1 - firstNum)/10;
     t$$anonymous$$rdNum = score1 - firstNum - secondNum;
     
 
     if(firstNum == 0 && secondNum >= 2 && pointTwo == true)
     {
         pointTwo = false;
         Debug.Log("play 0.2");
         sound(zero,two);
     }
     else if(secondNum == 5 && pointFive == true)
     {
         pointFive = false;
         if (firstNum == 0){
             sound(zero,five);
             }
         else if (firstNum == 1){
             sound(one,five);
             }
         else if (firstNum == 2){
             sound(two,five);
             }
         else if (firstNum == 3){
             sound(three,five);
             }
         else if (firstNum == 4){
             sound(four,five);
             }
         else if (firstNum == 5){
             sound(five,five);
             }
         else if (firstNum == 6){
             sound(six,five);
             }
         else if (firstNum == 7){
             sound(seven,five);
             }
         else if (firstNum == 8){
             sound(eight,five);
             }
         else if (firstNum == 9){
             sound(nine,five);
             }
         pointZero = true;
     }
     
     else if(pointZero == true && secondNum == 0)
     {
         pointZero = false;
         if (firstNum == 0){
             sound(zero,zero);
             }
         else if (firstNum == 1){
             sound(one,zero);
             }
         else if (firstNum == 2){
             sound(two,zero);
             }
         else if (firstNum == 3){
             sound(three,zero);
             }
         else if (firstNum == 4){
             sound(four,zero);
             }
         else if (firstNum == 5){
             sound(five,zero);
             }
         else if (firstNum == 6){
             sound(six,zero);
             }
         else if (firstNum == 7){
             sound(seven,zero);
             }
         else if (firstNum == 8){
             sound(eight,zero);
             }
         else if (firstNum == 9){
             sound(nine,zero);
             }
         pointFive = true;
     }
 }
 
 
 function sound(x,y){
     audio.clip = x;
     audio.Play();
     yield WaitForSeconds(.7);
     audio.clip = point;
     audio.Play();
     yield WaitForSeconds(.7);
     audio.clip = y;
     audio.Play();
     yield WaitForSeconds(.7);
     audio.clip = miles;
     audio.Play();
 }

...........................................................................................

And in the script "Score", the score is found with

................................................................................

 function score() {
     distanceScore += (Variables.actualFps + 88) / 5280;    
 }
     
 InvokeRepeating("score",1,1);

......................................................................................

T$$anonymous$$s code works up to when it needs to say "1.0". So it correctly says 0.2 and 0.5.

Comment
Add comment · Show 1
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 HuskyPanda213 · May 12, 2014 at 04:30 AM 0
Share

You can round the numbers using float. Also, use an array of audioclips, not several variables: var Sounds : audioclip[];

I am a C# coder, bit that should work.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by BillK88 · May 12, 2014 at 02:46 PM

Yes, you definitely should use an array for sound clips.

You could then refer to its elements by index.

Assuming you have audio clips in Sounds array (Sounds = [zero,one,two,...]), you could convert t$$anonymous$$s large part of code to somet$$anonymous$$ng smaller:

 if(firstNum == 0 && secondNum >= 2 && pointTwo == true)
 {
    pointTwo = false;
    Debug.Log("play 0.2");
    sound(zero,two);
 }
 else if(secondNum == 5 && pointFive == true)
 {
    pointFive = false;
    sound(Sounds[firstNum],five);
    pointZero = true;
 }
 else if(pointZero == true && secondNum == 0)
 {
    pointZero = false;
    sound(Sounds[firstNum], zero);
    pointFive = true;
 }
 

 

And it could be compacted a little bit more I suppose.

Best Regards

Frontend programmer for http://www.pneumatig.eu

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

How to get sound to play on certain condition (javascript) 2 Answers

Connecting 2 scripts(javascpt)? 2 Answers

JS Loop question 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