- Home /
Question by
Dylanpolis · Jul 02, 2017 at 02:01 AM ·
javascriptscripting problemscript.scriptingbasics
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();
}
}
}
Comment
Best Answer
Answer by aldonaletto · Jul 02, 2017 at 04:14 AM
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);
}
Answer by Vollmondum · Jul 02, 2017 at 06:23 AM
Why do you declare "c : collider" if your "c" is never used? :)
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.