Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Michael 20 · Apr 07, 2011 at 08:23 PM · audiogamepausemusicresume

how to pause and resume music after pausing game?

Hi

How can I pause the music when pausing the game and let it resume when I go back to the game?

I tried this script but the music resumes when I release the button.

function Update(){

if(Input.GetKeyDown(KeyCode.Escape)) {

audio.Pause();

}

if(Input.GetKeyUp(KeyCode.Escape)) {

audio.Play();

}

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

4 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by Axtrainz · Nov 22, 2016 at 10:57 PM

pretty simple solutions:

1- Assuming you using Time.TimeScale to pause your game you do this.

 void Update()
 { 
   if(Input.GetKeyDown(KeyCode.Escape))
    {
       if(Time.TimeScale > 0)
        {
             Time.TimeScale = 0;
              audio.Pause();
         }
        else
         {
                 Time.TimeScale = 1;
                  audio.Play();
          }
      }//input
 
 }//update

This will pause/unpause your game and the audio.


2- if you handle the pause game on some other script then just:

     void Update()
     { 
       if(Input.GetKeyDown(KeyCode.Escape))
        {
           if(audio.isPlaying())
                  audio.Pause();
            else
                   audio.Play();           
        }//input
     }//update




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
avatar image
1

Answer by Berawdu · Mar 13, 2014 at 08:52 PM

function Update () {
if (gamePaused == true) { audio.mute = true; }

 else if (gamePaused == false)
 {
 audio.mute = false;
 }

This is the best way I found to handle this situation.

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
avatar image
1

Answer by samsut · Nov 22, 2016 at 08:48 PM

@korial1

In unity 5 to resume a music after pause game you can use the function UnPause();

If the audio is in camera for example:

 if(Input.GetKeyDown(KeyCode.Escape)) {
 //audio.Pause();
 Camera.main.GetComponent<AudioSource>().Pause();
 }
 
 if(Input.GetKeyUp(KeyCode.Escape)) {
 //audio.UnPause();
 Camera.main.GetComponent<AudioSource>().UnPause();
 }

I'm using unity v5.3.5f1 and this works to me.

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 samsut · Nov 23, 2016 at 01:27 AM 0
Share

Can be the Play(), it works too. In my project the Play() was restarting the music, then I found the UnPause() function and it worked. Now I tried in another project, and the play is working just fine.

avatar image
-1

Answer by oliver-jones · Apr 07, 2011 at 08:27 PM

Just add a boolean system:

var gamePaused = false;

function Update(){

if(Input.GetKeyDown(KeyCode.Escape) && gamePaused == false){ gamePaused = true; } else if(Input.GetKeyDown(KeyCode.Escape) && gamePaused == true){ gamePaused = false; }

if(gamePaused == true){ audio.Pause(); }

else if(gamePaused == false){ audio.Play(); } }

Hope that works

Comment
Add comment · Show 4 · 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 Michael 20 · Apr 07, 2011 at 09:22 PM 0
Share

I tried it but the audio keeps playing after I pause the game

avatar image oliver-jones · Apr 07, 2011 at 09:41 PM 0
Share

in that case, you want to mute your Audio Listener, attached to your camera: AudioListener.volume = 0;

avatar image Spitfire47 · Oct 13, 2013 at 04:43 PM 0
Share

I tried it, but my audio doesn't even play

avatar image christoph_r · Nov 23, 2016 at 01:34 AM 0
Share

$$anonymous$$ake sure to properly format your code.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Music On/Off Switch 2 Answers

Different music for pause menu? 1 Answer

How would you make a Repeat-After-Me music game? 0 Answers

Audio keeps playing when paused 1 Answer

Problem with audio after leaving pause menu 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