Playing a sound file on keyup/down

OK, so bear with me here, i am a complete newb to Unity and am just trying to find my way, so the first thing i wanted to try was to get a sound to play on a keypress.

I am not getting any erros, but my sound is not playing at all.

The file is located in a Resources folder.

Here is my code:

public class FirstPage : MonoBehaviour {

    public AudioSource mySound;
   
    // Use this for initialization
    void Start () {
        mySound = Resources.Load("05 - Listen to the Man_[plixid.com].mp3") as AudioSource;
    }

    void Update () {
    if(Input.GetKeyUp(KeyCode.Space)){
            mySound.Play();

        };
    }


}

anyone point out what i am doing wrong please

thanks

slimer

i think the cast would be to AudioClip instead of to AudioSource
or
mySound.clip = Resources.Load("music.mp3") as AudioClip;but why not just drag the clip to the source in the editor?
then do not need to attempt to load anything or worry about folders

the source does not appear anywhere in my editor

i have a references folder with all the dll’s in

also, when i change it to AdioClip, it does not allow the function .Play()

as i said, i’m totally new at using this software, so you may need to elaborate a little more

You’re confusing AudioClip with AudioSource.

AudioClips are the sound files, which you need to add to an AudioSource, which will play them.

So something like this. This wouldn’t work though, because you can’t load mp3 files with Resources.Load. You can use the WWW class to load mp3 files on mobile platforms, but otherwise you’ll need to decode mp3 files somehow.

public class FirstPage : MonoBehaviour
{

    public AudioSource mySound;

    // Use this for initialization
    void Start()
    {
        AudioClip clip = (AudioClip)Resources.Load("05 - Listen to the Man_[plixid.com].mp3");
        mySound.clip = clip;
    }

    void Update()
    {
        if (Input.GetKeyUp(KeyCode.Space))
        {
            mySound.Play();

        };
    }


}

thanks for the advice

i’m missing something really obvious here though as the sound file just doesn’t play

i’ve imported a wav file instead, but it just doesn’t play

Wav won’t work either. Resources.Load can only load assets that have already been imported into your project.

Take a look at the mp3 importer in my signature. There’s also another one on the asset store.

Whenever sounds just won’t play, I always check the following things:

Is the original sound asset audible?

Does the asset play in the inspector?

Is the object playing the sound too far away from the camera to be audible?

Does the problem get fixed if I make the audiosource 2D with max priority?

Am I accidentally trying to trigger the sound on every frame?

Am I doing anything else to interrupt playback of the sound?

Finally, if I were you, I would just declare a public audio clip and drag the asset I want onto it in the inspector.

Try removing the file extension on Resources.Load().

It can be a bit cantankerous, especially between build/editor. Its best to have simple names so you can rule out formatting problems that the compiler might have changed for some reason.