Search Unity

[SOLVED] Playing sound on button with OnClick()

Discussion in 'UGUI & TextMesh Pro' started by CelticKnight, Nov 19, 2016.

  1. CelticKnight

    CelticKnight

    Joined:
    Jan 12, 2015
    Posts:
    378
    Hello Guys,

    Currently, I am using Unity 4.6.1f with a course I am doing and one thing i wanted to try and do was add an sound to when a button was clicked. And I happened to find this bit of advice on mentioned on these forums:
    However when I did this I didn't get any sound happening. I found a good sound at FreeSFX and would like to get this sound happening. So would anyone be able to guide me to get this working?

    Any help would be greatly appreciated.

    Thankyou and Regards,
    CK.
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @CelticKnight

    It should work that way.

    To be able to hear something you also have to have AudioListener.

    It should be located on Main Camera unless you have removed it accidentally or created a new camera (Actually new cameras too will have it...)
     
    Farahzhd likes this.
  3. CelticKnight

    CelticKnight

    Joined:
    Jan 12, 2015
    Posts:
    378
    Thankyou for the response.

    As I went through some other older threads earlier this morning, and I mean really early in the morning :p. I saw that someone mentioned that there needed be an Audio Listener attached to the main camera to be able to work. So that was my first port of call. It's definitely there, but, no dice with the sound playing. So, I have no idea work to make that process work. I will try the process again as Unity4.6 has been playing up something fierce since I stated using it again with this course.

    As this didn't work I have been entertaining the idea of creating a function in one of the scripts that will have a music file attached and playing the sound file through coding. It might work o_O, I still don't know enough about how to make a sound file work yet, but, it can't be that hard - famous last words :D.

    Regards,
    CK.
     
  4. JoaoPaul54

    JoaoPaul54

    Joined:
    Nov 20, 2013
    Posts:
    52
    Are you sure the audiosource is being listened by the audio listener? You have audio volume decay the further away the audiosource is from the listener.

    If your camera (which has the listener) is far away from teh canvas that might be the problem.
    You have a curve that defines the volume decay in the audiosource, try to crank it all the way up in any distance.
     
    CelticKnight likes this.
  5. CelticKnight

    CelticKnight

    Joined:
    Jan 12, 2015
    Posts:
    378
    Thanks so much for your input guys it's given me some insight into problem solving and things to keep in mind in future projects.

    But I think i figured out why the sounds weren't working, late last night, either by dropping them onto the playboard or through scripting. With images I have just been dragging or saving them directly into the assets subfolder and using them as needed. The same with sounds, putting them straight into the sound subfolder on Assets - so it looks like they weren't being properly imported :confused:!?! So I just changed the sounds to take off the 3D aspect, so I could access the "Apply" button after doing that the sounds starting magically working - dropping them onto the playboard or through script.

    Sorry guys it was a newbie problem :oops:.

    Warmest Regards,
    CK.