• 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
Question by byurocks23 · Mar 21, 2014 at 04:12 AM · javascriptaudioscoreincrement

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

What I want to happen is whenever the score $$anonymous$$ts a certain amount, I want a sound to play. T$$anonymous$$s is what I have:

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

Every time distanceScore goes up by 0.1, i want it to play a specific sound to that number (I already know how to play a sound so I don't need help with that). For example, when distanceScore = 0.1, I want it to play a sound. When it equals 0.2, I want it to play a seperate sound, when it equals 0.3, I want to play an even separate sound etc. How can I do t$$anonymous$$s?

Comment

People who like this

0 Show 0
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
Best Answer

Answer by RyanZimmerman87 · Mar 21, 2014 at 04:39 AM

One semi easy way to do it as far as understanding the logic would be to set up a float variable w$$anonymous$$ch corresponds to the score. Then every time you call the score function you can use the float variable to determine the condition checks.

Maybe somet$$anonymous$$ng like t$$anonymous$$s (in C#):

 float scoreFloat;
 
 void Start()
 {
 scoreFloat = 0;
 }
 
 void scoreFunction()
 {
 //calculate score
 distanceScore += (Variables.actualFps + 88) / 5280;    
 
 //first score condition for sound 1
 if (scoreFloat <= 0)
 {
 if (distanceScore >= .1f)
 {
 //set scoreFloat t$$anonymous$$s corresponds to the .1f
 scoreFloat = 1;
 
 //call sound function
 soundFunction();
 }
 }

 //all other score conditions for all other sounds
 else if (scoreFloat >= 1)
 {
 //you already got the distanceScore above 
 
 //create a new variable for your condition check
 float newScoreFloat = .1f + (scoreFloat/10);
 
 if (distanceScore >= newScoreFloat)
 {
 //set scoreFloat to use in the next scoreFunction()
 scoreFloat += 1; 
 
 //call sound function
 soundFunction();
 }
 }
 }
 
 
 //sound function
 void soundFunction()
 {
 //determine w$$anonymous$$ch sound to play based on the scoreFloat
 if (scoreFloat == 1)
 {
 //play sound 1
 }
 
 else if (scoreFloat == 2)
 {
 //play sound 2
 }
 
 else if (scoreFloat == 3)
 {
 //play sound 3
 }
 
 //etc
 }

I would try somet$$anonymous$$ng like t$$anonymous$$s, hope it helps. T$$anonymous$$s would only work though if it was impossible to go up two tiers of score sounds between just one scoreFunction()

Also sorry about the C# I never used JavaScript so wanted to make sure I didn't do somet$$anonymous$$ng stupid in t$$anonymous$$s example.

I t$$anonymous$$nk the logic could be improved a little but should be a pretty clear example.

Comment
byurocks23

People who like this

1 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 byurocks23 · Mar 21, 2014 at 05:23 AM 0
Share

It was a bit troubling to convert to JavaScript, but it worked great. Thank You

avatar image

Answer by Foestar · Mar 21, 2014 at 05:10 AM

So you're trying to play a separate sound for each time the score goes up? That's gonna require a ton of separate sounds. But if t$$anonymous$$s is what you really want to do then just create a variable and check it before playing the sound. Depending on the returned results play the desired sound. All in an if statement.

So for example:

     var soundplayed : float = 0;
     private var score : float =0;
     
     //enter your function here that will increase score and sound
     function scored () {
         score ++;
         soundplayed++;
         //or soundplayed=soundplayed+1;
         if (soundplayed=1){
             audio.Play();
         }else if(soundplayed=2){
             audio.Play();
         }
     }

The reason I would base the audio after a separate variable is because if you wanted to reset the sounds back to the beginning for some reason like if it was based on the score wit$$anonymous$$n a time limit like a combo. T$$anonymous$$s way you are still improving your variable score but also dealing with the sound wit$$anonymous$$n the same function.

Comment

People who like this

0 Show 0 · 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

Have readout of score at certain increments (JavaScript) 1 Answer

Play music trough scenes 0 Answers

Quick way to enter and condense repeated lines of code 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