Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in 'Audio & Video' started by GIitch, Jan 12, 2019.

  1. GIitch

    GIitch

    Joined:
    Aug 1, 2013
    Posts:
    24
    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

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,589
  3. GIitch

    GIitch

    Joined:
    Aug 1, 2013
    Posts:
    24
    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
    VengadoraVG and Brother_77 like this.
  4. Brother_77

    Brother_77

    Joined:
    Feb 8, 2019
    Posts:
    233
    Hello G. Did you get it to work ?
     
  5. mklitsner

    mklitsner

    Joined:
    Nov 2, 2018
    Posts:
    6
    I think you have to both play the audio from the audio source and run the transition script