Need help controlling Unity audio

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.

Could you post your script? Maybe there is an error you haven’t seen.

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:

    void Update()
      {
        if (!soundActive)
          {
            AudioSource.PlayClipAtPoint(myAudio, transform.position, 0);
          }
      }
    void FixedUpdate()
      {
        if (soundActive)
          {
            AudioSource.PlayClipAtPoint(myAudio, transform.position, sound);
          }
      }

The idea was to play a “zero” sound to turn the sound off - but that didn’t work either. I’m stumped.

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.

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.

private AudioSource myAudioSource;
void Start()
{
    myAudioSource = GetComponent<AudioSource>();
}

public void Play()
{
    if(myAudioSource.isPlaying == false)
    {
        myAudioSource.clip = myAudio;
        myAudioSource.Play();
    }
}

public void Stop()
{
    myAudioSource.Stop();
}

Thanks. I’ll try it and see how it goes.