Need help playing a sound

I must be doing something wrong, because I cannot play a sound via script.

I have an audio listener on the main camera.

I have a sound imported as an Audio Clip. I can hear the sound play one time

Would someone please just walk me through the process of making sure things are set up properly in the editor, then the corresponding 2-3 lines of script to play the sound?

I’m not real sure where the AudioSource goes in Unity. Does it go on the main player? What if he will be playing 20 sounds, do you just put that many AudioSources on the object?

How do you reference individual sounds and play them in script?

I must be missing a step somewhere.

Basically I have a wav file called CARTOON_BOUNCE.

Listener set to camera. Audio Source set to bouncing cube.

In script I have:

var CARTOON_BOUNCE : AudioClip;

then below when there’s a collision and I apply gravity upwards I want to play the sound and I have this:

function OnCollisionEnter(collision : Collision) {

	for (var contact : ContactPoint in collision.contacts) {
        
		if ( contact.otherCollider.name != "Wall") {
			rigidbody.AddRelativeForce (-200.0, 0, 0);
			
			manager.numJumps ++;	
			
			audio.PlayOneShot(CARTOON_BOUNCE); 

			break;
		}
}
   }

I just can’t get the sound to play.

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.

Got it sorted out. Turns out I missed assigning a sound in the inspector on the script on the object.

But then I discovered another issue. Sound works on the mac, but when I build and run on the iPhone it’s silent. Any thoughts?

Well, reimported the sound, uncompressed, and it worked fine. The iPhone doesn’t like compressed sounds? Will do some more forum search now :slight_smile:

iPhone can only play one compressed sound at a time, so the usual solution is to have the background music (which is usually the largest of the sounds) compressed and other sounds uncompressed.