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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Need help controlling Unity audio

Discussion in 'Audio & Video' started by TheWebExpert, Dec 1, 2016.

  1. TheWebExpert

    TheWebExpert

    Joined:
    Apr 14, 2015
    Posts:
    195
    I have a single-level game I've written using a tutorial online. The game works great, and has background music and sound effects. What I'm trying to do is to add in the functionality of sound volume and music volume, along with an on/off switch for either. So: If the sound is on, play it at level n.

    The primary difficulty with this specific game is that the objects all have their own Audio Sources. What I'd like to do is to create a script called PlaySound to attach to each item, with the public AudioClip mySound variable, so I can pass in the specific .wav sound needed. The sound needs to be played, in a loop, at the current volume, until (and unless) I turn OFF the sound. Then, the sound should stop IMMEDIATELY.

    Currently, if I turn off the sound and play, nothing seems to happen. If I quit the game & restart, the sound is off - but then turning it on does nothing, unless I quit and restart the game. The on/off and volume isn't having the immediate effect. Can anyone give me a working example? I have over a dozen independent objects. The things which generate their own sound (i.e., zombies dying & bullets) seem to be working fine - but they don't have their own AudioSources.
     
  2. BravenBitSoftware

    BravenBitSoftware

    Joined:
    Feb 26, 2014
    Posts:
    56
    Could you post your script? Maybe there is an error you haven't seen.
     
  3. TheWebExpert

    TheWebExpert

    Joined:
    Apr 14, 2015
    Posts:
    195
    There isn't an error; everything works fine. It's just that the tutorial designed this to have items with their own AudioSources. For instance, there's a fire. It has its own AudioSource, which loops continuously. What I want to do is take that and convert it to code - so that, if the sound is ON, it will loop continuously, at the volume level specified. I have objects which play a sound once (if the sound is ON), and those work fine; it's just that I can't figure out how to do looping sound properly with the code, and again, I'm not sure why the sound ON/OFF isn't immediate.

    This is what I'm trying at the moment:
    Code (CSharp):
    1.     void Update()
    2.       {
    3.         if (!soundActive)
    4.           {
    5.             AudioSource.PlayClipAtPoint(myAudio, transform.position, 0);
    6.           }
    7.       }
    8.     void FixedUpdate()
    9.       {
    10.         if (soundActive)
    11.           {
    12.             AudioSource.PlayClipAtPoint(myAudio, transform.position, sound);
    13.           }
    14.       }
    The idea was to play a "zero" sound to turn the sound off - but that didn't work either. I'm stumped.
     
  4. TheWebExpert

    TheWebExpert

    Joined:
    Apr 14, 2015
    Posts:
    195
    I'm still looking for help on this. Can anyone give any input?? This is REALLY frustrating! I can do single sounds, but not repeated sounds.
     
  5. SuperNeon

    SuperNeon

    Joined:
    Mar 16, 2014
    Posts:
    85
    You can play a looping sound with PlayClipAtPoint. It is used for one shot spatialized sounds.
    You should set settings you want in the AudioSource and call the method Play and Stop on it.

    For now, you are just calling each frame PlayClipAtPoint and it is really bad.
    You should try something like that.

    Code (CSharp):
    1. private AudioSource myAudioSource;
    2. void Start()
    3. {
    4.     myAudioSource = GetComponent<AudioSource>();
    5. }
    6.  
    7. public void Play()
    8. {
    9.     if(myAudioSource.isPlaying == false)
    10.     {
    11.         myAudioSource.clip = myAudio;
    12.         myAudioSource.Play();
    13.     }
    14. }
    15.  
    16. public void Stop()
    17. {
    18.     myAudioSource.Stop();
    19. }
     
  6. TheWebExpert

    TheWebExpert

    Joined:
    Apr 14, 2015
    Posts:
    195
    Thanks. I'll try it and see how it goes.