#pragma strict
var buttonInRange;
var buttonActivated;
var doorSound : AudioClip;
var target : Transform;
var player : GameObject;
function OnTriggerEnter(c : Collider)
{
buttonInRange =true;
}
function OnTriggerExit (c : Collider)
{
buttonInRange = false;
}
function Update ()
{
if(buttonInRange == true)
{
if(Input.GetKeyDown("e"))
{
player.transform.position = target.position;
GetComponent.<AudioSource>().Play();
}
}
}
Have you set the AudioSource clip property? I’ve noticed that you’re defining the AudioClip in doorSound, but it’s never assigned to the AudioSource.
You should have something like this:
if(Input.GetKeyDown("e"))
{
player.transform.position = target.position;
GetComponent.<AudioSource>().clip = doorSound;
GetComponent.<AudioSource>().Play();
}
Or, alternatively:
if(Input.GetKeyDown("e"))
{
player.transform.position = target.position;
GetComponent.<AudioSource>().PlayOneShot(doorSound, 1);
}
Why do you declare “c : collider” if your “c” is never used?
To make it work:
- Play() only works when some audio is already assigned to the gameobject’s audiosource in editor;
- PlayOneShot() works whenever without assignition;
- Make sure your door’s collider “Is Trigger” is on;
- Make sure your player, or whoever collides with the door has a Rigidbody attached, along with personal collider.