Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by wasicool7 · Aug 25, 2016 at 09:53 PM · audiosourcesavemute

How to save volume of audio source when game is restarted?

How do I save volume of my audio source (clicking the mute button) when game is restarted. You see I have one scene in my game and once the player dies there is a button the user can click on that resets the game, however it also resets the audio source, so when I play the game and click my mute button, the audio source volume is at 0 but when I die and click the reply button (the restart/reset button) the music is restarted as well, instead of staying muted (save the current volume). I tried using DontDestroyOnLoad(this) but my muted script is attached to my game over panel animation (the panel comes down when player dies) making the my game over panel animation not being destroyed when you restart the game. I tried other methods like putting my mute button script on a parent empty object and putting the object that has the audio source on under it as a child but it did the same thing. Anyway this is my mute button script:

 private bool mute; 

 public void Muted ()
 {
     mute = !mute;
     if (mute){
         gameObject.GetComponent<AudioSource>().volume = 0;
     }else
     {
         gameObject.GetComponent<AudioSource>().volume = 1;
     }
   }
 }

And this is my restart level when button is pressed (p.s. I used the OnClick method activating methods):

 public void NavigateTo(int scene)
 {
     Application.LoadLevel ("Game Level");
 }

Thank you :)

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by thor348k · Aug 27, 2016 at 07:13 AM

You can save it to PlayerPrefs:

 PlayerPrefs.SetInt("Volume", gameObject.GetComponent<AudioSource>().volume);

And then load it at the start of the scene:

 gameObject.GetComponent<AudioSource>().volume = PlayerPrefs.GetInt("Volume");

Here's how I would do it:

 private bool mute;
 private AudioSource source;
 
 private void Start()
 {
     // get the source, so you don't have to "Get" it each time
     source = GetComponent<AudioSource>();
     
     // if the volume has been saved before
     if (PlayerPrefs.HasKey("Volume"))
     {
         // set the volume to saved volume
         source.volume = PlayerPrefs.GetInt("Volume");
     }
 }
 
 public void Mute()
 {
     mute = !mute;

     if (mute)
     {
         source.volume = 0;
     }
     else
     {
         source.volume = 1;
     }
 
     // save the new volume
     PlayerPrefs.SetInt("Volume", source.volume);
 }

This is UNTESTED, I'm just typing in-browser. It's incomplete, but it will get the basic job done. For example, if the game is muted/saved, and then closed, when this is run when the game is opened, the game would be muted, or at least the audio source. You'd need to account for things like that.

If you don't want it to be persistent, you can use static variables, which are something like "global variables". Those won't be saved when the games closes, but will be kept while the game is running.

Here is a good Unity Tutorial on statics.

Good luck, hope this helps! :)

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

69 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 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 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

Problems with audio muting 1 Answer

How to mute multi-able audioclips within a single audiosource? 0 Answers

mute button not working after scene is reloaded 1 Answer

How to start audio clip on mute then click mouse button to unmute without disrupting clip time? Then click again to mute again all while audio clip remains on loop. 0 Answers

Unity2D: Muting audio Clip problems 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