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
1
Question by Entyro · Sep 04, 2013 at 10:30 AM · audiocoroutinepause

Audio problem

Hi there!

I have a question about my paus script. When i press escape the game pauses and the time is set to 0 and all audio stops playing. When i press escape, a button sound needs to play, but if wont because of "AudioListener.volume = 0;". It plays when i exit the pause menu because it's set to 1 when i press a second time. I've tried to use "yield WaitForSeconds" but it looks weird. Does anyone know how to paus every sound in the level except the button sound?

 var paused : boolean = false;
 var Target1 : GameObject;
 var Target2 : GameObject;
 var Target3 : GameObject;
 var texture : GUITexture;
 var pause : AudioClip;
 
 function Update ()
 {
 
     Pause();
 
 }
     
 function Pause()
 {
 
     if(Input.GetKeyDown("escape") && paused == false)
            {
            
             AudioSource.PlayClipAtPoint(pause, transform.position);
             //yield WaitForSeconds(0.3);
             paused = true;  
             Time.timeScale = 0;
             savedTimeScale = Time.timeScale;
             doPauseWindow = true;
             Time.timeScale = 0;
             AudioListener.volume = 0;
             Target1.GetComponent("MouseLook").enabled = false;
               Target2.GetComponent("MouseLookCamera").enabled = false;
               Target3.GetComponent("CrosshairSimple").enabled = false;
               gameObject.Find("PausetextureBlack").guiTexture.enabled = true;
               Screen.showCursor = true;
              Screen.lockCursor = false;
              texture.enabled = true;
             }
     
                    else if(Input.GetKeyDown("escape") && paused == true) {
            
                        paused = false;
                        Time.timeScale = 1;
                       AudioListener.volume = 1;
                       AudioSource.PlayClipAtPoint(pause, transform.position);
                       Target1.GetComponent("MouseLook").enabled = true;
                       Target2.GetComponent("MouseLookCamera").enabled = true;
                       Target3.GetComponent("CrosshairSimple").enabled = true;
                       gameObject.Find("PausetextureBlack").guiTexture.enabled = false;
                       Screen.showCursor = false;
                      Screen.lockCursor = true;
                      texture.enabled = false;
        
     }   
      
 }
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 aldonaletto · Sep 04, 2013 at 01:00 PM

The best solution is to set/clear AudioListener.pause instead of controlling the volume: this property is intended to be used exactly in these cases, and you can set the property ignoreListenerPause in the AudioSources that you want to continue playing.

Doing this in your current script requires that you use a dedicated AudioSource, since you don't have any reference to the temporary AudioSource created by PlayClipAtPoint. Add an AudioSource to the same object that contains the AudioListener (usually the main camera) and set audio.ignoreListenerPause at Start - like this (script attached to the camera):

 var paused : boolean = false;
 var Target1 : GameObject;
 var Target2 : GameObject;
 var Target3 : GameObject;
 var texture : GUITexture;
 var pause : AudioClip;
 
 function Start(){
     audio.ignoreListenerPause = true;
 }
 
 function Update()
     // if ESC pressed, toggle pause:
     if(Input.GetKeyDown("escape")) paused = !paused;
         audio.PlayOneShot(pause); // play the pause on/off sound
         if (paused){ // freeze/unfreeze time
             Time.timeScale = 0;
         } else {
             Time.timeScale = 1;
         }
         // set the several variables according to the paused variable:
         doPauseWindow = paused;
         Target1.GetComponent("MouseLook").enabled = !paused;
         Target2.GetComponent("MouseLookCamera").enabled = !paused;
         Target3.GetComponent("CrosshairSimple").enabled = !paused;
         gameObject.Find("PausetextureBlack").guiTexture.enabled = paused;
         Screen.showCursor = paused;
         Screen.lockCursor = !paused;
         texture.enabled = paused;
     }
 }





Comment
Add comment · Show 8 · 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 Entyro · Sep 04, 2013 at 01:27 PM 0
Share

When I try this and test the level the sound is playing all the time. And I get an error "Field 'UnityEngine.AudioSource.ignoreListenerPause' not found."

avatar image Entyro · Sep 05, 2013 at 06:27 AM 0
Share

Do you have any idea what causes the problem?

avatar image Sisso · Sep 05, 2013 at 11:33 AM 1
Share

@RiseX, what is your unity3d version? I think that it is a new property released in 4.1.

Whatever, appear to have some bugs in AudioSource static variables. If your solution is working now, try to stay there until you have better alternatives. If not , like @aldonaletto said, you need to play your audio in different AudioSource, one for runtime where you set volume to 0 when paused, one for audios that play paused.

avatar image aldonaletto · Sep 05, 2013 at 11:02 PM 0
Share

@Sisso is right: this is a new feature, not available in Unity 3.x

avatar image Entyro · Sep 06, 2013 at 08:16 AM 0
Share

ok, but I have Unity 4.0

Show more comments

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

18 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

Related Questions

Pause Audio Help 1 Answer

Pause script errror. 1 Answer

Is it possible to query the Unity (3.0) transform tree from a secondary thread? 1 Answer

Play audio on keyboard click? 1 Answer

How do I gaplessly change music clips in a script? 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