Sounds on button click (or just sounds on click)

UPDATED WITH COMPLETE SOLUTION:
In order to get a sound to play on a mouse click, the simplest solution is located here:
http://answers.unity3d.com/questions/857810/play-audio-from-46-ui-button-on-click-script.html

This will get your button sound working. I had to add a delay scene change because the sound latency was so high that the sound would not play in time (the scene changed too quickly.) This code explains how to do that:

private AudioSource audioSource;
private float audioTimer;
public void LoadLevel(string name)
{
    StartCoroutine(LoadLevelTimer(audioTimer, name));
    
}
public void Next(string name)
{
    StartCoroutine(LoadLevelTimer(audioTimer, name));
    
}

void Start()
{
    audioSource = this.GetComponent<AudioSource>();
    audioTimer = audioSource.clip.length;
}

IEnumerator LoadLevelTimer(float audioTimer, string name)
{
    yield return new WaitForSeconds(audioTimer);
    SceneManager.LoadScene(name);
}

Finally, I had to change an Audio Setting under Edit > Project Settings > Audio: Set DSP Buffer Size to full performance. This caused the audio to play without delay. If you want to play the audio on a mouse click (any mouse click), this code will suffice:

private AudioSource audioSource;
void Start()
{
    audioSource = this.GetComponent<AudioSource>();
}
void Update()
{

    if (Input.GetMouseButtonDown(0))
    { 
      audioSource.Play();
    } 

Below is the original post:

I have created a new scene called Menu and added a Start Button to it that the user clicks to move into the actual game for my version of Text101. However, every tutorial and attempt I have made to play a sound on the button click has failed. These include the tutorials in the following places (EDIT: deleted two links, left the correct link):

None play the sound when the button is clicked. Is it possible the sound is not playing before the scene changes?

EDIT: Discovered that the last link offers the correct solution. However my button sound would not play because the scene would change too quickly. See below.

If so, wait for the audio to finish:

audioSource.Play();

Invoke (“LoadNextLevel”, audioSource.clip.length);

void LoadNextLevel ()

{	levelManager.LoadLevel ("XX_WhateverScene_XX");

}

1 Like

Alright so here was my attempt to implement a delay but it did not give me a sound on a button click.

private AudioSource audioSource;
private float audioTimer;
public void LoadLevel(string name)
{
    StartCoroutine(AudioLength(audioTimer));
    SceneManager.LoadScene(name);
}
public void Next(string name)
{
    StartCoroutine(AudioLength(audioTimer));
    SceneManager.LoadScene(name);
}

void Start()
{
    audioSource = this.GetComponent<AudioSource>();
    audioTimer = audioSource.clip.length;
}

void Update()
{
	if(Input.GetKeyDown(KeyCode.Escape))
    {
        SceneManager.LoadScene("Start Menu");
    }
	else if(Input.GetKeyDown(KeyCode.Q))
    {
        Application.Quit();
    }
}	

IEnumerator AudioLength(float audioTimer)
{
    yield return new WaitForSeconds(audioTimer);
}

The loadscene have to be inside the IEnumerator after the yield return new waitforseconds, otherwise it wont wait until loading the next scene. Btw, you could also add the sound to a persistent gameobject and make the button call a method to play it inside it

1 Like

Could you show me an example? I’m at my wit’s end with this.

1 Like

public void LoadLevel(string name)
{
StartCoroutine(AudioLength(audioTimer));
}
IEnumerator AudioLength(float audioTimer)
{
yield return new WaitForSeconds(audioTimer); SceneManager.LoadScene(name);
}

Just swap it as the example above and it should work. I am away from my computer in order to show an example of how to do it using persistent game objects

1 Like

Ok yes I’ve made this change and now the CoRoutine is working correctly. It was an issue of the sound not being able to play because the scene changed too quickly. However, there is a noticeable delay from when the button is clicked to when the sound plays. I’m looking into how to fix this new issue. Thank you both for your help @Freddie_G and @Joao_Dalvi

1 Like

Strange, can you send a print of the button inspector? You can add an On Click event that communicate directly with the Audio Source and give you the option to choose the premade Play() method, don’t even have to add a script reference to the audio

1 Like

Yes that is what I have done here:

It is attached to the GameManager GameObject here:

The original issue was that the scene would load before the sound played. Now I know why: the sound doesn’t play for almost a full second or two after the button is clicked and released.

Edit: Changed DSP Latency to lowest latency available. No effect.

1 Like

I also just discovered that I can play the sound on a mouse click instead of on a button click. However, the audio is still delayed in playing. I went and edited the sound file and removed some of the beginning empty sound area but it has not helped.

1 Like

How do you make it play on mouse click instead of button click?
Sorry, this time I don’t have a clue regarding what could be causing this problem :confused:
If you find the solution I would be happy to know too

1 Like

@Joao_Dalvi It was incredible simply:

if (Input.GetMouseButtonDown(0))
{ 
    audioSource.Play();
}
1 Like

Fixed it! Changed the DSP Setting under Edit -> Project Settings -> Audio to “Best Performance.”

Full solution to button click forthcoming.

1 Like

But don’t it call the button sound regardless if you click on the button or not?
Very good to know regarding this audio performance option!! Thanks!

1 Like

That’s correct. Maybe some people prefer it. Thanks for your help again!

1 Like

Privacy & Terms