Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Can AudioSources not play sound when their methods are called by a UnityEvent .Invoke()?

Discussion in 'Scripting' started by Pinkuboxu, Apr 18, 2018.

  1. Pinkuboxu

    Pinkuboxu

    Joined:
    Mar 20, 2014
    Posts:
    54
    I have an object with an AudioSource and it's playback methods work fine with calls from other scripts, Button OnClick() events. However, when I try to make a public UnityEvent that can do the same and be set in the inspector, the method does get called and it will do everything else but the sound never happens. I'm not getting any errors either.
     
  2. Pinkuboxu

    Pinkuboxu

    Joined:
    Mar 20, 2014
    Posts:
    54
    I had to implement the following work around:

    Code (CSharp):
    1.     public class SoundEffectsManager : MonoBehaviour
    2.     {
    3.         public AudioSource aSource;
    4.         public AudioClip clip01;
    5.         public bool play = false;
    6.  
    7.         private void Update()
    8.         {
    9.             if (play)
    10.             {
    11.                 play = false;
    12.  
    13.                 aSource.PlayOneShot(clip01);                  
    14.             }
    15.         }
    16.  
    17.         // Call this from the public UnityEvent in the inspector
    18.         public void SetClipAndPlay( AudioClip clip)
    19.         {
    20.             clip01 = clip;
    21.             play = true;
    22.         }
    23.  
    24.         // this won't work if you call it from the public UnityEvent
    25.         public void PlaySFX( AudioClip clip)
    26.         {
    27.             print("Playing: " + clip); // this appears in the console though
    28.             aSource.PlayOneShot(clip);
    29.         }
    30.     }
    Actually, this stopped working as well. It's weird enough to just be a bug I think.
     
    Last edited: Apr 18, 2018
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,186
    You should report that as a bug!
     
  4. Pinkuboxu

    Pinkuboxu

    Joined:
    Mar 20, 2014
    Posts:
    54
    Ah, thanks! I was almost certain it should work that way.
     
  5. Pinkuboxu

    Pinkuboxu

    Joined:
    Mar 20, 2014
    Posts:
    54
    I put together a sample of my problem and sent it in. I'd have to check if the problem persists in 2017.4 though, I'm still on .3.