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 /
avatar image
0
Question by javierfed · May 08, 2019 at 04:13 AM · audioaudiosourceaudioclipplayoneshot

2d PlayAudioOneShot inconsistent behavior

I am working on a game where I have prefabbed menus with buttons on them. each button calls into my singleton MainAudio, triggering PlaySoundEffectInstant (line 90) passing in the sound effect for button click.

MainAudio plays sound effects on one audiosource while music plays on a seperate AudioSource so that I may fade music in and out without interupting sound effects.

My problem occurs when I navigate between these menus, eventually the PlayOneShot stops working. I have Visual Studios Debugger running and can verify the calls to PlaySoundEffectInstant does indeed occur, it gets all the way to the line PlayOneShot(audioclip, volume)(line 59) and then... nothing. when it goes to another menu sometimes it works, sometimes nothing... but it gets all the way to that line. I don't know what is wrong. there is nothing that changes the volume or priority of the audio sources.

 using DigitalRuby.Tween;
 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MainAudio : MonoBehaviour
 {
     private static MainAudio instance = null;
 
     public AudioSource sourceMusic = null;
     public AudioSource sourceEffects = null;
 
     private AudioClip defaultClip = null;
 
     [Range(0.0f, 1.0f)]
     public float sfVolume = 1.0f;
     public float FadeDelay = 1;
 
     static Coroutine FadingIn = null;
     static Coroutine FadingOut = null;
 
     private void Awake()
     {
         if (instance == null)
         {
             instance = this;
             defaultClip = sourceMusic.clip;
 
             DontDestroyOnLoad(gameObject);
         }
         else
         {
             sourceMusic.Stop();
             Destroy(gameObject);
         }
     }
 
     /// <summary>
     /// Plays Music or sound effects immidiately
     /// </summary>
     /// <param name="audio"></param>
     /// <param name="asSoundEffect"></param>
     /// <param name="volume"></param>
     public static void Play(AudioClip audio)
     {
         instance.PlayMusic(audio);
     }
 
     /// <summary>
     /// Plays audio one shot for sond effects
     /// </summary>
     /// <param name="audio"></param>
     /// <param name="volume"></param>
     private void playSoundEffect(AudioClip audio, float volume = 0.0f)
     {
         if (!IsMuted())
         {
             instance.sourceEffects.PlayOneShot(audio, (volume <= 0.01f ? instance.sfVolume : volume));
         }
     }
 
     /// <summary>
     /// 
     /// </summary>
     /// <param name="audio"></param>
     /// <param name="volume"></param>
     public static void PlaySoundEffect(AudioClip audio, float volume = 0.0f)
     {
         instance.StartCoroutine(instance.SoundEffectCoroutine(audio, volume));
     }
 
     private IEnumerator SoundEffectCoroutine(AudioClip audio, float volume = 0.0f)
     {
         instance.playSoundEffect(audio, volume);
         yield return new WaitForEndOfFrame();
     }
 
     public bool PlayingClip(AudioClip clip)
     {
         return sourceMusic.clip == clip;
     }
 
     public static bool IsPlayingClip(AudioClip backgroundMusic)
     {
         return instance.PlayingClip(backgroundMusic);
     }
 
     public void PlaySoundEffectInstant(AudioClip audio)
     {
         instance.playSoundEffect(audio, instance.sfVolume);
     }
 
     public void PlayMusic(AudioClip audio)
     {
         if (!IsMuted() && instance.sourceMusic.clip != audio)
         {
             instance.sourceMusic.Stop();
             instance.sourceMusic.clip = audio;
             instance.sourceMusic.Play();
         }
     }
 
     public static bool IsMuted()
     {
         return PlayerPrefs.GetInt(LS.Common.TermStrings.AllAudioOn, 1) == 0;
     }
 
     public static void ToggleMuted(bool value)
     {
         PlayerPrefs.SetInt(LS.Common.TermStrings.AllAudioOn, value ? 1 : 0);
         PlayerPrefs.Save();
 
         instance.TogglePauseMusic(!value);
     }
 
     public void ToggleMutedInstance(bool value)
     {
         ToggleMuted(value);
     }
 
     public void TogglePauseMusic(bool isPaused)
     {
         if (sourceMusic.clip != null)
         {
             if (isPaused && sourceMusic.isPlaying)
             {
                 sourceMusic.Pause();
             }
             else if (!isPaused)
             {
                 sourceMusic.UnPause();
             }
         }
     }
 
     /// <summary>
     /// Plays new Music clip by fading from one to another.
     /// </summary>
     /// <param name="clip">New Music to play</param>
     public static void PlayWithFade(AudioClip clip)
     {
         instance.StartCoroutine(instance.FadeToMusic(clip));
     }
 
     private IEnumerator FadeToMusic(AudioClip clip)
     {
         yield return FadeOut();
         sourceMusic.Stop();
         sourceMusic.clip = clip;
         sourceMusic.Play();
         yield return FadeIn();
     }
 
     public static void FadeMusicOut()
     {
         if (FadingOut == null)
         {
             if (FadingIn != null)
             {
                 instance.StopCoroutine(FadingIn);
                 FadingIn = null;
             }
 
             FadingOut = instance.StartCoroutine(instance.FadeOut());
         }
     }
 
     public static void FadeMusicIn()
     {
         if (FadingIn == null)
         {
             if(FadingOut != null)
             {
                 instance.StopCoroutine(FadingOut);
                 FadingOut = null;
             }
 
             FadingIn = instance.StartCoroutine(instance.FadeIn());
         }
     }
 
     private IEnumerator FadeOut()
     {
         if (sourceMusic.volume <= 0.0f)
         {
             FadingOut = null;
             yield break;
         }
 
         FloatTween tween = TweenFactory.Tween(null, sourceMusic.volume, 0, FadeDelay, TweenScaleFunctions.Linear, null);
 
         sourceMusic.volume = 1.0f;
 
         while (!tween.Update(Time.deltaTime))
         {
             sourceMusic.volume = tween.CurrentValue;
             yield return new WaitForEndOfFrame();
         }
 
         sourceMusic.volume = 0.0f;
         FadingOut = null;
     }
 
     private IEnumerator FadeIn()
     {
         if (sourceMusic.volume >= 1.0f)
         {
             FadingIn = null;
             yield break;
         }
 
         FloatTween tween = TweenFactory.Tween(null, sourceMusic.volume, 1, FadeDelay, TweenScaleFunctions.Linear, null);
 
         sourceMusic.volume = 0.0f;
 
         while (!tween.Update(Time.deltaTime))
         {
             sourceMusic.volume = tween.CurrentValue;
             yield return new WaitForEndOfFrame();
         }
 
         sourceMusic.volume = 1.0f;
         FadingIn = null;
     }
 }
 

alt text

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

0 Replies

· Add your reply
  • Sort: 

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

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

Sound wont play when triggered (javascript) 1 Answer

Why PlayOneShot is not working? C# 1 Answer

WWW Audio and PlayOneShot: Playing the same AudioClip Twice Cuts off First Instance 1 Answer

Need help with AudioSources 1 Answer

How to fix my reload sound? 0 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