Sound with OnCollisionEnter?

Alright, I know this has been posted before, but for the life of me I can’t seem to get this to work. And it seems like this should be simple. I’m pretty new at this, so bear with me.

I have a door, and when my first person controller collides with it, I want a sound to occur. I have tried using an OnTriggerEnter function with a box collider, and it works, but I don’t want the sound to go off when he is going THROUGH the open door as well, only when he is colliding with the closed door.

I feel like an OnCollisionEnter would be a good bet, but it doesnt work; the sound does not go off. How come it goes off with an OnTriggerEnter and not with OnCollisionEnter? Is there some setting I am missing?

I have this script:

#pragma strict

var PressSpace : AudioClip; 

function OnColliderEnter(o:Collider){
	Debug.Log("The trigger fired");
	
	audio.PlayOneShot(PressSpace);
}

Any insights would be much appreciated!

I don’t know if this is the best solution, but what I would do is just set a Boolean flag on the door telling whether it is open or closed. In your OnCollisionEnter, check that value and if the door is closed, play the sound… otherwise ignore it.

Hmm. There has to be a simpler way of doing this. Is there a particular setting that needs to be enabled with OnCollisionEnter functions that I might be missing? It seems like a simple piece of code :confused:

Your code calls an OnColliderEnter(), but there is no such thing. It’s OnCollisionEnter(). It’s not getting called because it doesn’t exist.

You could also disable the collider when the door is open. I don’t know how you could get much simpler.

fire7side, that was my bad initially. I have actually used the correct function (OnCollisionEnter) and still, nothing happens.

Dustin Horne: Could you do the same thing even for a OnTriggerEnter function? If so how, based on this script:

var PressSpace : AudioClip; 

function OnTriggerEnter(o:Collider){
	Debug.Log("The trigger fired");
	
	audio.PlayOneShot(PressSpace);
}

Thanks so much guys.

I’ve also tried something like this, and it still doesn’t work. The soundclip has been added to the script in the inspector as well as an audio source in the inspector. I don’t know what I could be doing wrong :confused:

var PressSpace : AudioClip; 

function OnCollisionEnter (collision : Collision) {

    audio.PlayOneShot(PressSpace);

}

Does it work if you just call it from the start function. Also, put a debug in the OnCollisionEnter and make sure it’s being called. Double check all spelling.

I tried putting a Debug.Log in the OnCollisionEnter function and it’s not showing up in my console so I guess it isn’t being called?

And spelling is all correct :confused: