Here’s a script to play an imported sound clip:
public var audioClipObj: AudioClip;
public var soundVolume: float = 0.0;
public var soundPitch: float = 0.0;
public var loopSound: boolean = false;
private var gameObj: GameObject;
private var audioSrcObj: AudioSource;
public function Start() {
this.gameObj = new GameObject("TempSoundGameObject");
this.audioSrcObj = this.gameObj.AddComponent(AudioSource);
this.audioSrcObj.clip = this.audioClipObj;
this.audioSrcObj.playOnAwake = false;
this.audioSrcObj.volume = this.soundVolume;
this.audioSrcObj.pitch = this.soundPitch;
this.audioSrcObj.loop = this.loopSound;
return;
}
public function PlaySound() {
if (!this.audioSrcObj.isPlaying) {
this.audioSrcObj.Play();
}
return;
}
public function StopSound() {
if (this.audioSrcObj.isPlaying) {
this.audioSrcObj.Stop();
}
}
public function PlaySoundOneShot() {
this.audioSrcObj.PlayOneShot(this.audioClipObj, this.soundVolume);
return;
}
Add the script to a game object in your scene, specify the audio clip in the inspector, and the set the desired volume and pitch values and the boolean value to indicate whether to loop the sound.
Then call the PlaySound and PlaySoundOneShot functions to play the sounds. You can construct more elaborate scripts to fade in/fade out soundtrack music, etc., but this should get you started.
Regarding your other questions, if your game is using stereo sounds (see below) I recommend putting together a “scene sound player” that contains a set of “sound objects”, one each per sound you want to play. Each sound object can be constructed from an empty game object with an audio source component added to it as well as a script like the above. The sound game objects can then parented to one top level empty game object and then then that object can be made a prefab. Also attached to the parent sound player object is a script which contains a reference variable for each child sound object, and functions to play them. Then for each of your scenes, all you have to do is drag an instance of the sound player prefab into the scene, set a reference to it (i.e. to the controller script contained in the parent game object) within your game scripts and play the sounds calling functions on that script. This keeps all of your audio sources in one place and makes it trivial to change them as you develop your game. The beauty of making a prefab scene player is that, if you have 20 scenes in your game where a particular sound is being played for a purpose, and you decide you want to use a different sound, you just need to change the clip in the prefab, not in 20 different game scenes.
Regarding the audio listener, there can only be one in the scene. Whether you need to attach it to the camera depends on whether you are playing mono or stereo sounds (either native stereo clips or ones that you “forced” to stereo when you imported them). For mono sounds, where how the sounds will be heard depends on where they are played relative to the listener, it is common to attach the listener to camera. For stereo sounds it doesn’t matter. The Unity manual discusses stereo versus mono sounds.
For mono sounds, I would make a sound object for each as described above and make that object a prefab that can either be instantiated by dragging it into the scene in the editor or via script. You could also modify the sound player described above and add some factory functions that construct and return instantiated sound object prefab instances, but that’s probably overkill. Making the sound objects prefabs is important, though, since it will save a lot of trouble when you want to swap a clip.