Hey, I’m new to Unity so please go easy on me. Anyways, I am using this script for a light effect (Flickers lights):
var flickerSpeed : float = 0.07;
private var randomizer : int = 0;
while (true) {
if (randomizer == 0) {
light.enabled = false;
}
else light.enabled = true;
randomizer = Random.Range (0, 1.1);
yield WaitForSeconds (flickerSpeed);
}
I made my own custom sound of flickering lights (its in .wav by the way if thats needed), and its very short so it has time to be played more then once and quick. So is there any way I can make the sound go off whenever the light flickers the same as the script? Thanks in return.
var flickerSpeed : float = 0.07;
var sound:AudioClip;
private var randomizer : int = 0;
function Update() {
Flicka();
}
function Flicka(){
if(!audio.isPlaying){
if (randomizer == 0) {
light.enabled = !light.enabled;
audio.Play();
}
randomizer = Random.Range (0, 11);
}
}
I change a little bit, maybe you will get back to original but keep in mind that a while(true) with no break command will simply never stop running keeping the rest of the game on hold.
Drag and drop your audio to the sound slot in the Inspector.
I am assuming it works but I cannot try it now…
Edit: I removed some parts so that now every frame your light has 10% chance of switching on/off. That also means it can flick real fast or it can stay on real long. Change the random value to make the chnage happen more often. (0,2) = 50% (0,5) = 20% adnd so on. Note that the extreme value is excluded when using integer (0,2) will only returns 0 or 1.
See if that suits your effect.
Second edit: now the switching cannot happen if the sound is playing.