Its not playing the Audio?

#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? :slight_smile:

To make it work:

  1. Play() only works when some audio is already assigned to the gameobject’s audiosource in editor;
  2. PlayOneShot() works whenever without assignition;
  3. Make sure your door’s collider “Is Trigger” is on;
  4. Make sure your player, or whoever collides with the door has a Rigidbody attached, along with personal collider.