Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We've updated our Terms of Service. Please read our blog post from Unity CTO and Co-Founder Joachim Ante here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Audio How would I call an Audio Mixer Snapshot in a script?

Discussion in 'Audio & Video' started by GIitch, Jan 12, 2019 at 6:29 AM.

  1. GIitch

    GIitch

    Joined:
    Aug 1, 2013
    Posts:
    14
    If this is how standard Audio Sources can be used, how might I use a Snapshot in a similar context? I get the feeling this isn't what Audio Mixer was intended for, but I'm curious to see if it's possible.

    Code (CSharp):
    1. public class PlayerAudio : MonoBehaviour {
    2.  
    3.     public AudioMixerSnapshot lightAttack;
    4.     public AudioMixerSnapshot heavyAttack;
    5.     public AudioMixer playerCombat;
    6.  
    7.     void Update()
    8.     {
    9.         if (Input.GetKeyDown(KeyCode.Mouse1))
    10.              GetComponent<AudioSource>().Play();
    11.  
    12.         if (Input.GetKeyUp(KeyCode.Mouse1))
    13.             GetComponent<AudioSource>().Stop();
    14.     }
     
  2. Peter77

    Peter77

    Joined:
    Jun 12, 2013
    Posts:
    3,020
  3. GIitch

    GIitch

    Joined:
    Aug 1, 2013
    Posts:
    14
    Thanks, that helped a little bit. But I'm still not sure I understand how to use them. I selected correct the snapshot within the script in Unity's inspector, but it still doesn't play the audio on button press.

    I got the audio to work on button press when I was working with the standard Audio Source, and I had no issue activating Snapshots with triggers. But this function is giving me grief. What am I doing wrong?

    Code (CSharp):
    1.     public AudioMixerSnapshot lightattack;
    2.  
    3.     void Update()
    4.     {
    5.         if (Input.GetKeyDown(KeyCode.Mouse1))
    6.             lightattack.TransitionTo(.01f);
    7.     }
     
    Last edited: Jan 12, 2019 at 11:31 AM