AudioSource problems

Hi guys,

i got a script for item picking wich should make a sound when i pick an item:

static var bouleCount : int = 10;
var CollectSound : AudioClip;

function OnControllerColliderHit(hit:ControllerColliderHit){
	
	if(hit.gameObject.tag == "boule"){
		
		Destroy(hit.gameObject);
		/
		bouleCount++;
		
		audio.PlayOneShot(CollectSound);
		
	}	
	
}

@script RequireComponent (AudioClip)

when i compile there is no error message, but when i run the game and collect the item the sound dont plays and i got this error message showing :
“MissingComponentException there is no audio source attached to the first person controler but a script is trying to acces it.”
the fact is i attached the sound via the inspector as usual. strangely, the only way to get it working is by attaching the sound directly to the first person controller.

anyway i can make the sound play via the script as usual ?

thank you,

PARADOKS

That’s not true. You just haven’t attached an Audio Source. Dragging a clip onto a game object is a shortcut to adding an Audio Source and assigning a clip to it, which is why that method worked for you.

(Technically, you can have more than one Audio Source attached to a Game Object – though I don’t recommend it – and when you drag the clip onto the game object, it is assigned to the Audio Source on top of all the others. In the case no Audio Source is there, then one is created first.)

What you actually want is this:

@script RequireComponent (AudioSource)

An Audio Clip isn’t even a component. I don’t know why @script RequireComponent (AudioClip)
doesn’t throw an error; but it certainly doesn’t do anything.

…I just logged a bug about this. It seems that as long as you use a Unity Object, no error or warning is given for RequireComponent, even though it becomes useless. Thanks for bringing it to the attention of the community.

thanks, but now the result is exactly the same as with my “strange” method:

i delete the attached sound and the script
i relink the modified script with the “audiosource” change, and when i add the script to my object it automaticaly adds an audio source component in top of it.

does that mean that if i got to use several sound i will have to scroll kilometers in my inspector ?

No. That’s what PlayOneShot is for. You can use a single audio source and play as many clips as you want with it until Unity dies (which should be very, very many, at least outside of the iPhone; I’ve never had it happen). However, every time you play a sound with PlayOneShot, the audio waveforms are added together, which can result in distortion if your clips are loud to begin with, and you haven’t lowered the Audio Source’s volume parameter.

If you plan on having a lot of clips play at the same time, then it will be best to use multiple Audio Sources, and lower their volumes as necessary. However, I wouldn’t use multiple Audio Sources on one Game Object, because as you said, it looks messy. I’d use as many Game Objects as I wanted the limit of sounds to be, use one Audio Source for each, keep track of how many clips were playing, and lower each of their volumes as I wanted. I’d also use Play() instead of PlayOneShot(), because that would make it so that you’d never have more clips playing than audio sources. The old sounds would get immediately cut off, and that’s never good, but if you’ve got a ton of sfx going, a pop probably won’t even be audible.

As it is, You might want to do what I just described, even if you don’t want to get into scripting volume, whose math can get complicated if you want predictable results. For example, if it’s only important to be able to play three sounds at once, but it’s possible that five could happen at once, maybe only use three Audio Sources and use Play() for each of them. This is how synthesizers work - you only get so much “polyphony”; the standard thing to do is to just cut off the earliest notes you played, if they’re still sustaining.

All of this pain-in-the-butt stuff will become unnecessary if Unity ever gets a compressor component, but this is the only way to do it, for now. :x

If your clips are percussive, though, you probably won’t even have a problem. Hopefully that is your case.

Damn me!

You made me understand, thank you very much Jessy.

Hmmm:

I had the same error when I used the script:
var levelToLoad : String;
var normalTexture : Texture2D;
var rollOverTexture : Texture2D;
var beep : AudioClip;

function OnMouseEnter (){
guiTexture.texture = rollOverTexture;
}

function OnMouseExit (){
guiTexture.texture = normalTexture;
}

function OnMouseUp () {
audio.PlayOneShot(beep);
yield new WaitForSeconds(0.35);
Application.LoadLevel(levelToLoad);
}

@script RequireComponent(AudioSource)

I then, with PlayBtn selected in the hierarchy, added the audio via the inspector to the new variable “beep” but it still had the same MissingComponent issue. Finally, after looking at this, I dragged the audio clip onto the PLayBtn in the hierarchy. It now sounds upon play of game and in the way intended in the script. Why does it play at the start of game play?

thanks!

You probably have the box Play On Awake clicked in your AudioSource. Just unclick it and it will stop playing at the start of the level.