Lights shutting down with sound
Hi, Im beginner with Unity, and even more with scripting i mostly modify the scripts i found. What im trying to achieve is lights shuting down one after another, with sound effect connected to every shutdown of the light.
Light are shuting down correctly but I don't hear the sound with second shutdown of the lamp. Any ideas what im missing?
#pragma strict var light_down_0 : GameObject; var light_down_1 : GameObject; var light_down_2 : GameObject; var light_down_3 : GameObject; var light_down_4 : GameObject; var played = false; var trig = false; var clickSound: AudioClip; /////When player enters trigger/////// set to true/////// function OnTriggerEnter (other : Collider) { %|1512355362_1|% } //////Enable renderer and trigger sound and timer///// function Update () { %|971779091_2|% %|86938754_3|% %|-166982660_4|% %|-571540330_5|% } //// timer //// function removeovertime () { %|-1436862808_6|% %|100986914_7|% %|36113627_8|% %|1401870186_9|% %|-516028577_10|% %|-825324713_11|% %|1599428303_12|% %|-445450465_13|% %|1633754797_14|% %|-1105708243_15|% %|2036316252_16|% %|-913393898_17|% %|-2138975816_18|% %|-1390553392_19|% %|-1545792410_20|% %|1846181786_21|% } //// sound ///// function makehimscream_0 () { %|-1171808759_22|% %|-1183411412_23|% %|-2045067513_24|% %|-1710635996_25|% } function makehimscream_1 () { %|-451771264_26|% %|-1682377517_27|% %|7164834_28|% %|1675370969_29|% } function makehimscream_2 () { %|-1052795000_30|% played = true; %|2104514616_32|% %|1598251606_33|% } function makehimscream_3 () { %|384067722_34|% %|500527016_35|% %|323764729_36|% %|1087399600_37|% } function makehimscream_4 () { %|-539729754_38|% played = true; %|1249881886_40|% %|1928674998_41|% }
#pragma strict
var light_down_0 : GameObject;
var light_down_1 : GameObject;
var light_down_2 : GameObject;
var light_down_3 : GameObject;
var light_down_4 : GameObject;
var played = false;
var trig = false;
var clickSound: AudioClip;
/////When player enters trigger/////// set to true///////
function OnTriggerEnter (other : Collider) {
trig = true;
}
//////Enable renderer and trigger sound and timer/////
function Update () {
if (trig == true) {
light_down_0.GetComponent.<Light>().enabled = false;
removeovertime ();
}
}
//// timer ////
function removeovertime () {
makehimscream_0 ();
yield WaitForSeconds (3);
light_down_0.gameObject.SetActive (false);
makehimscream_1 ();
yield WaitForSeconds (3);
light_down_1.gameObject.SetActive (false);
makehimscream_2 ();
yield WaitForSeconds (3);
light_down_2.gameObject.SetActive (false);
makehimscream_3 ();
yield WaitForSeconds (3);
light_down_3.gameObject.SetActive (false);
makehimscream_4 ();
yield WaitForSeconds (3);
light_down_4.gameObject.SetActive (false);
}
//// sound /////
function makehimscream_0 () {
if (!played) {
played = true;
light_down_0.GetComponent.<AudioSource>().PlayOneShot(clickSound);
}
}
function makehimscream_1 () {
if (!played) {
played = true;
light_down_1.GetComponent.<AudioSource>().PlayOneShot(clickSound);
}
}
function makehimscream_2 () {
if (!played) {
played = true;
light_down_2.GetComponent.<AudioSource>().PlayOneShot(clickSound);
}
}
function makehimscream_3 () {
if (!played) {
played = true;
light_down_3.GetComponent.<AudioSource>().PlayOneShot(clickSound);
}
}
function makehimscream_4 () {
if (!played) {
played = true;
light_down_4.GetComponent.<AudioSource>().PlayOneShot(clickSound);
}
}
Answer by CapitalLlama · Aug 31, 2016 at 02:31 AM
You are assigning the global variable "played" to true on the first sound. Then the rest check if played. Since played = true still, they never play.
Glancing at your code I don't think you even need to check played since you are making separate function calls and the audio isn't looping.