- Home /
How to make buttons have sound when it is highlighted and clicked?
I also want the sound when it's highlighted to be different from the sound when it's clicked. So I first got two different sounds and named them Highlighted and Clicked.Next, I added them as audio sources to a button named 1-player.Then, I made a script named Button and typed the following in it.
using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; using System.Collections;
public class Button : MonoBehaviour, IPointerEnterHandler,IPointerDownHandler { private AudioSource audioSource; public AudioClip Clicked; public AudioClip Highlighted;
public void OnPointerEnter(PointerEventData ped) {
audioSource.Play (Clicked);
}
public void OnPointerDown(PointerEventData ped) {
audioSource.Play (Highlighted);
}
}
Note: I copied someone else's code that was in response to a question similar to mine.I changed some of it because my methods and functions were different from the person's (which caused compiler errors.)However, even after changing the code, I still have compiler errors.Can anyone please tell me how to fix my code (or fix or change anything) so that when I highlight and click my button it will produce sound? Any help will be greatly appreciated!
Your answer
Follow this Question
Related Questions
[C#] Unity asks for a } for a reason I don't know ( '}' expected ) 1 Answer
Calling an Audio Source on one game object from a script on another game object..? 1 Answer
How to make a Panel (Or Scrollbar) Appear on Button Click 0 Answers
How to set a default value for Drop-downs, but not display that value in the actual list? 0 Answers