- Home /
JavaScript Play Audiosource Problem
I must be doing something wrong and am sure there is a simple solution but I've been searching for hours and every answer I find either throws up errors or Unity tries to update the script and fails. I can't seem to get my audiosource to play in this script. Any help would be much appreciated this is driving me crazy!
#pragma strict
var rb: Rigidbody;
var PoliceSplash : AudioSource = GetComponent.<AudioSource>();
var PoliceDestroyed : int;
function Start () {
rb = GameObject.Find("Nightbrake Police Unit").GetComponent.<Rigidbody>();
}
function OnTriggerExit(collider : Collider)
{
if(collider.tag == "Water")
{
//EnableSplashPrefab
//Play Splash Sound Effect
yield WaitForSeconds(1);
rb.isKinematic = true;
print("A Police Car has driven into the water.");
PoliceDestroyed = PoliceDestroyed + 1;
}
}
function Update () {
}
Answer by Wolfrik_Creations · Jun 17, 2016 at 07:28 AM
I don't know if I'm insane, or stupid, but there I see no call to play the AudioSource.
Try under your if(collider.tag == "Water"), add PoliceSplash.Play();
#pragma strict
var rb: Rigidbody;
var PoliceSplash : AudioSource = GetComponent.<AudioSource>();
var PoliceDestroyed : int;
function Start () {
rb = GameObject.Find("Nightbrake Police Unit").GetComponent.<Rigidbody>();
PoliceSplash = GetComponent.<AudioSource>();
}
function OnTriggerExit(collider : Collider)
{
if(collider.tag == "Water")
{
//EnableSplashPrefab
PoliceSplash.Play();
yield WaitForSeconds(1);
rb.isKinematic = true;
print("A Police Car has driven into the water.");
PoliceDestroyed = PoliceDestroyed + 1;
}
}
function Update () {
}
Hhaaahaha oh wow, I'm the stupid one can't believe that was the problem. Thanks!
Happy to help! Don't worry, this and anything similar happens to me all the time.
Your answer
Follow this Question
Related Questions
Why does the audio sound different than the original ? 0 Answers
Play audio at consistent volume 2 Answers
Sound wont play when triggered (javascript) 1 Answer
Looping an audio while holding "shift key" 1 Answer
Unity audio solution 1 Answer