1
\$\begingroup\$

I'm trying to reproduce a sound clip on the AudioSource of a game object by using the .Play method. What i'm trying to do is a card game of sorts, but with each card being a different section of a song of a genre. I combine different cards with different genres and when i press play, the combination should start playing, but now when the play button is pressed, only noise comes out. The code for the start method looks like this:


public class AudioStart : MonoBehaviour
{
    AudioSource thisAudioSource;
    
    
    
    // Start is called before the first frame update
    void Start()
    {
        thisAudioSource = this.GetComponent<AudioSource>();
    }

    // Update is called once per frame
    void Update()
    {
        if (playButton.playPressed) {
            thisAudioSource.Play();
        }
        else {
            thisAudioSource.Stop();
        }
    
    }
}

The way i'm doing it is by having a button that on click changes a bool to true or false depending if there is sound playing (the truth check on the if statement). The AudioSource outputs to an AudioMixer on a specific group under the master group (which group depends on the slot that the card is dropped in). The AudioStart script is attached to different game objects with a different audio clip each. I've also noticed that when i try to reproduce two cards, only one AudioSource gets played. Any help would be appreciated. Thanks!

\$\endgroup\$
3
  • \$\begingroup\$ > "using the .Play method": Your code snippet shows PlayOneShot. Without parenthesis, it won't even compile. Are you using Play or PlayOneShot? It's hard to diagnose a bug if you don't share the actual code with the bug. \$\endgroup\$
    – Jim U
    Nov 8 '20 at 20:26
  • \$\begingroup\$ I'm sorry, i didn't notice that. I tried PlayOneShot to see if it worked (read about that in some forum), but i got nothing, and it seems i forgot to rewrite it. Fixed it now. Thanks. \$\endgroup\$
    – Morganuz
    Nov 8 '20 at 20:57
  • \$\begingroup\$ Welcome. You are constantly calling Play() inside Update(). As far as I remember, Play() will start from beginning of audio clip each time it is called. Try this: if (playButton.playPressed && !thisAudioSource.isPlaying) \$\endgroup\$ Nov 9 '20 at 14:41
0
\$\begingroup\$

PlayOneShot() will start playing the audio every time the function is called, so calling it in your Update() function will cause the clip to start playing every single frame. You get noise because the audio clip is playing playing multiple times simultaneously.

Solution

    void Update()
    {
        if (playButton.playPressed)
        {
            if (!thisAudioSource.isPlaying)
                thisAudioSource.Play();
        }
        else {
            thisAudioSource.Stop();
        }
    
    }
\$\endgroup\$
1
  • \$\begingroup\$ Now it works. Thank you! \$\endgroup\$
    – Morganuz
    Nov 8 '20 at 21:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.