- Home /
Two OnClicks on one button. One doesnt work all the time.
I have a button, it leads to a new scene but I also want it to make a noise when clicked. I have two different scripts, one for changing scenes and one for making the clicking noise. The weird part is that the noise works but only sometimes.
Any suggestions?
Answer by Razputin · Apr 15, 2016 at 04:12 AM
void OnClick()
{
StartCoroutine(DoTheThing())
}
Private IEnumerator DoTheThing()
{
//Do your first thing
yield return new WaitForSeconds(1.5f)
//Do your Second thing
}
Answer by jgodfrey · Apr 14, 2016 at 11:58 PM
I don't think the execution order of multiple OnClick events is guaranteed. So, i bet your problem is related to timing. Sometimes, the scene is probably changing before the sound is played.
You can probably solve the issue by calling a single OnClick method that does both things - in the order you want.
public void OnClick()
{
GetComponent<AudioSource>().Play();
Scene$$anonymous$$anager.LoadScene("Settings");
}
I assume the sound plays consistently if you comment out the new scene load (in the single OnClick handler)? If that's the case, the problem must still be that the scene is changing before the sound has had a chance to play. If that's the case, you might want to allow a short delay between the two calls.
Hmmm... So, helping you to figure out what the problem was and then suggesting exactly how to fix it wasn't enough. I guess I needed to actually write the code as well... ;(
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Button only works once 1 Answer
triggering an animation on click? 1 Answer
Deleting a prefab with onclick/touch 1 Answer