add sound effects to cursor

hello. im making a game and im trying to figure out how to do this myself since programmers in my team are inactive. so, sorry if this is a dumb question but i want to know how to add a sound effect to a cursor, because i want to:

  1. add sound effect for normal click.
  2. add sound effect for hovering on buttons.
  3. add sound effect for clicking a button.

ok so, i know now that i have to add an “audio source” to the game object by googling abit.
but further than that, i can’t reach.
to make it more clear, this is my game and i want to add a sound effect to when you click “new game”, and when you hover over “new game”, “continue”, “options”, “credits”, and “quit”.
if you want to help me further than just adding the sound effect to the cursor it would be highly appreciated.

i tried a few codes from google and youtube but there’s an error on the “add.listener” idk what to do.

using UnityEngine;
using System.Collections;
[RequireComponent(typeof(UIButton))]
public class Sound_Effects : MonoBehaviour
{
    public AudioClip sound;
    private UIButton button { get { return GetComponent<UIButton>(); } }
    private AudioSource source { get { return GetComponent<AudioSource>(); } }
    void Start()
    {
        gameObject.AddComponent<AudioSource>();
        source.clip = sound;
        source.playOnAwake = false;
        button.onClick.AddListener(() => PlaySound(();
    }
    void PlaySound()
    {
        source.PlayOneShot(sound);
    }

    }

Line 14 has a syntax error (should be ..PlaySound())..), which is probably the error you’re getting.

Since you’re not used to scripting, it may be easier to use the Inspector to configure some of this.

These things:

source.clip = sound;
source.playOnAway = false;
button.onClick.AddListener(PlaySound);

Can all be done in the inspector. Here’s a simpler version of that script:

using UnityEngine;
using System.Collections;
[RequireComponent(typeof(UIButton))]
public class Sound_Effects : MonoBehaviour
{
    public AudioClip sound;
    private AudioSource source;

    void Start()
    {
        source = gameObject.GetComponent<AudioSource>();
    }

    void PlaySound()
    {
        source.PlayOneShot(sound);
    }

}

Attach this to the button object. Now go to your Audio Source that you mentioned earlier:

Drag your sound clip into ‘AudioClip’ and make sure ‘Play On Awake’ is not checked.

Go to your UI Button:

Click the + under On Click. You’ll get one of these:

2847984--208007--upload_2016-11-10_10-28-44.png

Drag the button object itself into the lower-left field, and from the upper-right dropdown box find the above script (Sound_Effects), and from there choose PlaySound().

It might be a lot to take in, but give it a shot. Note that this will only give you sound effects when you click, but it’s a start.

2847984--208007--upload_2016-11-10_10-28-44.png

thanks alot for explaining this to me and being so patient with me! one of the programmers took some time today to make a script for me, but now i atleast know how to do it myself! i will definetely remember this. i still need to add alot of sound effects after in case i can’t find a programmer to help me within a month. it would be really awesome if you could explain some further about the adding sound effects like i have no clue how to add a sound effect to a specific button (like, after something is opened. not just the click of a button. maybe that is different??), or when landing for a jump for example. but i understand it will be very complicated to explain.

The only reason it’s complicated is that how to do it varies wildly with the context. However, generally speaking, you can use this same method here.

In the method above, you set up a function that actually plays the sound, and you tell Unity to run that function when the button is clicked.

For a less specific case, you can add the code I showed you (and an Audio Source) to any object with a script that needs to play a sound, and insert PlaySound(); in the script where you want it to happen. Just trying to follow this basic instruction set will definitely make you run into problems eventually, but it is a way to get started. Specific questions can always be posted here.

By the way, I’m not sure what you meant by “after something is opened”.

well not sure how to explain. in my game for example when you click “new game” u open the character selection.

I’m pretty sure you can just do the same thing in that case. Since pressing the button is what triggers the character selection view, can you just play the sound effect in OnClick() like before?