How to add a sound effect on a Door?

I am pretty new to Unity and I am doing a project on making a character to move around.
I have successfully move the character around by using Character Controller and manage to open and close the door by pressing the F and G keys respectively.

The problem now I faced is that I can’t seem to put a open and close door sound effect to the door…
I have the following javascripts:

var smooth = 2.0;
var DoorOpenAngle = 90.0;
var DoorCloseAngle = 0.0;
var open : boolean;
var enter : boolean;
var DoorOpen : AudioClip;
var DoorClose : AudioClip;

//Main function
function Update()
{
	if(open == true)
	{
		var target = Quaternion.Euler (0, DoorOpenAngle, 0);
		//Dampen towards the target rotation
		transform.localRotation = Quaternion.Slerp(transform.localRotation, target, Time.deltaTime * smooth);
	}
	
	if(open == false)
	{
		var target1 = Quaternion.Euler (0, DoorCloseAngle, 0);
		//Dampen towards the target rotation
		transform.localRotation = Quaternion.Slerp(transform.localRotation, target1, Time.deltaTime * smooth);
	}
	
	if(enter == true)
	{
		if(Input.GetKeyDown("f"))
		{
			(open) = true;
		}
		else if(Input.GetKeyDown("g"))
		{
			(open) = false;
		}
	}
}

//Activate the Main function when player is near the door
function OnTriggerEnter (other : Collider)
{
	if(other.gameObject.tag == "Player")
	{
		(enter) = true;
	}
}

//Deactivate the Main function when player is away from the door
function OnTriggerExit (other : Collider)
{
	if(other.gameObject.tag == "Player")
	{
		(enter) = false;
	}
}

var Trigger : AudioClip;
//When player enters the trigger zone
function OnTriggerStay()
{
	if(Input.GetKeyDown("f"))
	{
		if (open) audio.PlayOneShot(DoorOpen);
		else audio.PlayOneShot(DoorClose);
	}
}

The sound is just not playing or anything…Please do help me… Thank you very much!!

It is not a good idea to mix Input and Physics.

OnTriggerStay is done by the physics engine while Input comes fro your OS.

Both are not running the same speed. GetButtonDown returns true only on the frame you are pressing, if the physics is not run on that frame, nothing happens.

I would suggest to modify your code, so that the Input is taken care in the Update and the Trigger is using Enter and Exit to set a boolean on and off.

var isIn:boolean = false;
function OnTriggerEnter()
{
    isIn = true;
}
function OnTriggerExit()
{
    isIn = false;
}

function Update()
{
    if(Input.GetKeyDown("f") && open && isIn){
        audio.PlayOneShot(DoorOpen);
    }
}

Problem solved… it happened to be a misplace of the Audio Source… It should be placed under the parent not the child Door…
and I’ve changed the scripts of the OnTriggerStay function slightly:

function OnTriggerStay()
{
    if(Input.GetKeyDown("f"))
    {
       audio.PlayOneShot(DoorOpen);
    }
    if(Input.GetKeyDown("g"))
    {
       audio.PlayOneShot(DoorClose);
    }
}

and now it works perfectly fine with opening and closing the door~ :slight_smile:
Thank you for the help guys~