NastyDiaper
NastyDiaper

Reputation: 2558

AudioSource not playing in Unity3d when EventSystem is in Scene

It took me a while to find it, I even had to re-create the scene and I happened to forget the EventSystem but... I am developing a simple 2D app. Push a button and it plays a random sound. This works, super easy. There's a Button UI element and the OnClick calls my script (which is on an object) and that script plays the audio.

I wanted to add a Pitch slider so the script assigns the audio clip to the AudioSource and I attached the UI Slider's OnValueChanged to my script. Now I realized I need to add my EventSystem but now my buttons won't play the Audio!

I have Debug.Log statements right before the Play() call so I know the button callback is working there's just no audio. Once I delete the EventSystem, the buttons play the audio just fine but now I can't use my slider. Ideas?

Using 2018.2 - Android Build

EDIT

private AudioSource audioSource;

....

IEnumerator PlayClip() {
    // Do stuff

    audioSource.clip = audioClip;
    audioSource.Play();
    while(audioSource.isPlaying)
        yield return new WaitForSeconds(0.5f);
}

public void ButtonPushed() {
    StartCoroutine(PlayClip());
}

public void ChangePitch(float pitch) {
    audioSource.pitch = pitch
}

Then the Slider OnValueChanged() just calls ChangePitch with a range -3 to 3. All the code above is just one script and attached to one Controller object.

Don't worry, the Coroutine logic is a little better than that :)

Upvotes: 1

Views: 796

Answers (1)

Alex Myers
Alex Myers

Reputation: 7155

Your issue could be related to this Unity issue:

[AUDIOSOURCE] AUDIO WITH NEGATIVE PITCH DOESN'T PLAY WHEN LOOP IS DISABLED

User shouldn't use negative pitch value, unless one wants the clip to play backwards in the loop. If user wants it to play once through backwards, firstly he/she needs to set the position with AudioSource.time to the end of the clip and then play it.

I ran a test myself and I was able to reproduce your issue under the following conditions:

  • Looping is disabled on the audio source.
  • The pitch starts at a negative value.

If the starting value of the pitch is positive, or if it is negative with looping enabled, then I can use the slider to move the pitch between -3 and 3 without a problem.

Upvotes: 1

Related Questions