- Home /
Audio In Key Script Not Working
I have created a key and door. The door only opens when the key is obtained by the player. The player has to press "E" to obtain the card. When the player presses "E", I want a sound played. My script isn't working so I was hoping someone could be able to help.
#pragma strict
var TheKey : GameObject;
private var playerNextToKey = false;
var KeySound : GameObject;
function Update ()
{
if (Input.GetKeyDown(KeyCode.E) && playerNextToKey == true)
{
TheKey.active = false;
}
}
function OnTriggerEnter (theCollider : Collider)
{
if (theCollider.tag == "Player")
{
playerNextToKey = true;
KeySound.audio.Play();
}
}
function OnTriggerExit (theCollider : Collider)
{
if (theCollider.tag == "Player")
{
playerNextToKey = false;
KeySound.audio.Play();
}
}
Answer by screenname_taken · Jun 12, 2014 at 05:30 AM
Couple of things. The sound will activate when the player is in the trigger box and not when it presses "E". That's where you have the audio.Play.
The key statement won't work because you are using .active. To disable a whole gameObject use TheKey.SetActive(false); instead. (And true to enable it back).
Your answer
Follow this Question
Related Questions
I want my trigger sound only to play once! 0 Answers
Play sound on mouse enter 1 Answer
Java Script: Clock script help. 1 Answer
Locked door and key to open it script 1 Answer
Walking animation problem. 0 Answers