Adding sound to a script trigger
Hello,
I got small Jump scare script that I would like to add sound to it and destroy it after one trigger. I added the variable but i just can't figure out how to get it to work. I would appreciate any help given. :)
Below is the javascript.
#pragma strict
var Triger_Object : GameObject;
var Triger_Sound : AudioClip;
function Start () {
Triger_Object.SetActive(false);
}
function OnTriggerEnter () { Triger_Object.SetActive (true); }
function OnTriggerExit () { Triger_Object.SetActive(false); }
Answer by Cepheid · Feb 01, 2016 at 04:27 PM
You can use the AudioSource.PlayOneShot() function to play an AudioClip on your object's AudioSource. This is shown in the Scripting API linked below.
AudioSource.PlayOneShot - Unity Scripting API
Hope this helps! :)
Simply declare an audioSource reference and play an audioclip through the AudioSource.PlayOneShot() function. It would look like the following:
var Triger_Object : GameObject;
var Triger_Sound : AudioClip;
var Triger_AudioSource : AudioSource;
function Start () {
Triger_Object.SetActive(false);
}
function OnTriggerEnter () {
Triger_Object.SetActive (true);
Triger_AudioSource.PlayOneShot(Triger_Sound);
}
function OnTriggerExit () {
Triger_Object.SetActive(false);
Destroy (Triger_Object);
}
I hope that clears it up! :)
Off i see what you did. Bestially telling the script to use Audiosource to play the Audioclip.
I'm not home now but once i get home i'll try this.
Worked. thx alot. Now i'm going to try to put a timer but that's going to be another question if i can't figure it out first in another post.
:)
Answer by kenny1007 · Feb 01, 2016 at 06:08 PM
Thx but i'm still no understanding.
I did figured out how to destroy the object but i still can't get the audio to play.
var Triger_Object : GameObject;
var Triger_Sound : AudioClip;
function Start () {
Triger_Object.SetActive(false);
}
function OnTriggerEnter () {
Triger_Object.SetActive (true);
}
function OnTriggerExit () {
Triger_Object.SetActive(false);
Destroy (Triger_Object);
}
Your answer
