load audio files with Resources.Load and play them

I cant get it to work. I want to load three audio files at the beginning of a script and play them later responding to mouse clicks or touches. But I don’t get it to work!

I fiddled around with AudioSource and AudioClip but nothing actually works. It seems that I always have to connect the audio source to a Object.

testcode:

//Audios
var aReleaseCube:AudioClip;
var aLiftCube:AudioClip;
var aCubeOnSlot:AudioClip;

function StartMainScript()
{
	
	
	aLiftCube = Resources.Load("sounds/cube_up",AudioClip);
	aReleaseCube = Resources.Load("sounds/release_cube",AudioClip);
	aCubeOnSlot = Resources.Load("sounds/cube_on_slot",AudioClip);
	aCubeOnSlot.Play();
	//define some Levels
	LoadLevel(currentLevel);
	
	
}

There is no Play() function on audio clips; only on AudioSource. You might think “I don’t want all that crap!”, but Unity forces you to use an AudioSource anyway. I use a source created as a component on my Player GameObject.

First, load the audio clips:

AudioClip clip1 = (AudioClip) Resources.Load("Sounds/cube_release");
AudioClip clip2 = Resources.Load<AudioClip>("Sounds/cube_up");
AudioClip clip3 = Resources.Load("Sounds/cube_onslot", typeof(AudioClip)) as AudioClip;

Note that I’m using slightly different code to load each of the three clips. All of those formats will work; they all do the same thing. You can use whichever one you like best. There’s JS equivalents for each of these methods.

Next, you need an AudioSource. I normally attach this to a GameObject somewhere, using the editor. You don’t need three audio sources. And, for correct 3D positioning (whether you just want left/right balance or more sophisticated 3D positioning), it’s easier to just set up one AudioSource and set its properties in the editor. Yes, you could instantiate it using a script as well. Your choice.

Finally, to play the sounds, use the PlayOneShot function.

audioSource.PlayOneShot(clip1);

This way, you can play a wide array of clips through one audio source.

You might be using Resources.Load incorrectly, if I read the manual right from Unity - Scripting API: Resources.Load , it should be one of

aCubeOnSlot = Resources.Load("sounds/cube_on_slot",typeof(AudioClip));
aCubeOnSlot = Resources.Load("sounds/cube_on_slot") as AudioClip);

Then, you need to have a source, but you said it yourself.

And then it seems there might be an issue with Resources being auto-loaded as 3D clips, see this post for info:

If I understand correctly, you want to be able to play any of these audio clips at the same time. For that you’ll need 3 separate audio sources. Write a script that does this:

audio.clip = aCubeOnSlot;
audio.Play();

and attach it to 3 separate game objects with audio sources. Then put it inside of a public function that you can call from another script.

Previous answers are correct but remember to put your audio files inside Resources folder!

Audio file:

Assets/Resources/Sounds/card.mp3

Code:

AudioSource sndSource = gameObject.AddComponent <AudioSource>() as AudioSource;
AudioClip sndClick = Resources.Load<AudioClip>("Sounds/card");

sndSource.PlayOneShot (sndClick);

A couple pointers (although I’m not sure if they will solve your problem):

  1. You have to put your audio files into StreamingAssets folder, IIRC
  2. You have to specify full path (i.e. with extension) when loading them

Add three Audio Sources to gameObject and specify the clips you want to play and uncheck Play on Awake on all.

var audioSource:AudioSource;

In you StartMainScript() method
get the AudioSource attached to gameObject

funtion StartMainScript() // Your have to call this method on Start()

{

audioSourse = gameObject.getComponent(AudioSource);

}

In your Update() method add these line to respond to left click

if(Input.getMouseButtonDown[0]) // this respond to left mouse down

{

audioSourse.play();

}

You can try the simple:

AudioSource audio = gameObject.AddComponent < AudioSource > ();

audio.PlayOneShot ((AudioClip)Resources.Load (“gunshot1”));