I need help getting a sound to play when the E Key is pressed. (E will also load a new Scene).
#pragma strict
var doorSound : AudioClip;
var newScene : String;
function Start()
{
if(Input.GetKey(KeyCode.E))
GetComponent.<AudioSource>().Play();
}
function Update(){
if(Input.GetKey(KeyCode.E))
{
Application.LoadLevel(newScene);
}
}
Answer by Ali_Jaffer · Nov 21, 2015 at 11:53 AM
change update function by this
function Update(){
if(Input.GetKey(KeyCode.E))
{
GetComponent<AudioSource>().PlayOneShot(doorSound);
Application.LoadLevel(newScene);
}
}
I'm now getting and unexpected token error Expecting ) found "." in the line for GetComponent().PlayOneShot(doorSound);
Sorry I'm new at this
Replace :
GetComponent.<AudioSource>().PlayOneShot(doorSound); //Notice the extra dot.
You put it in in your own code but looks like it was forgotten in a copy-paste operation ;)
Thanks my bad! I'm always wake for days slaving away at script and I get on here when I'm desperate for answers. So I must have missed that.
Answer by bathorsthroat · Nov 22, 2015 at 12:29 AM
Thanks mate! This worked. I have one issue though. my Audio Clip is a recording of a door opening and the after a second or two the sound of a door closing. It seems that the audio is cut of or ended after the new scene is loaded. I's there anyway around this with making an audio trigger enter?
Your answer
Follow this Question
Related Questions
How to use a key as trigger? 2 Answers
How to make my Power Button stop my Conveyor Belt 0 Answers
Trigger when holding key 1 Answer