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 Astralage · May 24, 2015 at 02:45 PM · c#playermusicfadevolume

How to make volume continually go down in C#Script ?

Hello everyone !

I've been searching on this website for a few days now and haven't quite found what I was looking for, so I hope one of you will be able to save me !

Here's what I'm looking for, I have an audioclip inside a script, playing music for my main scene. What I want to do is to make the music fade out all the time because in my game, when the volume of the music gets to zero, it's Game Over. But the player can raise the volume up by collecting some sort of "power-up" objects, and the music fades out again until he finds another object, and so on until the player finishes the level.

I hope I've been clear enough, here's what my code looks like for now, it's attached to my player, thank you !

 using UnityEngine;
 using UnityEngine.Audio;
 using System.Collections;
 
 public class Audio : MonoBehaviour {
 
 
     public AudioSource Mainaudio;
     public float volume = 0f;
 
     // Use this for initialization
     void Start () {
 
         Mainaudio.Play ();
 
     }
 
 
     void OnTriggerEnter2D(Collider2D other){
 
         if (other.gameObject.tag == "Power Up") {
 
 
         }
 
     }
 
     void Update () {
     
 
         Mainaudio.volume = 0.5f;
         
 
     }
 }
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 TwinPrime · May 24, 2015 at 02:49 PM

   IEnumerator goDownforWhat ()
       {
         
           yield return new WaitForSeconds (0.3f);
           mainaudio.volume -= 0.5f;
        
       }
Comment
Add comment · Show 3 · 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 Astralage · May 24, 2015 at 06:58 PM 1
Share

Hahaha excellent it worked thank you very much ! :)

avatar image TwinPrime · May 24, 2015 at 08:21 PM 0
Share

pleasure is $$anonymous$$e

avatar image redeemer · May 24, 2015 at 11:35 PM 1
Share

Please, mark this as the correct answer so we know it requires no more attention, thanks :)

avatar image
0

Answer by venhip · May 24, 2015 at 11:33 PM

Just for anyone else coming across this later, another solution would be as follows:

 void Update(){
     Mainaudio.Volume -= Time.deltaTime * speedMultiplier;
 }

This would make the volume decrease by 1 for each second that passes, if speedMultiplier == 1. If speedMultiplier == 2, then for each second that went past it would reduce by 2 volume points, etc.

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Need help with my slider 3 Answers

Multiple Cars not working 1 Answer

Music play throughout all scenes? 1 Answer

Next song problem (music player) 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