- Home /
flickering lights when player gets near
Hi, I am making a simple horror game in Unity3D and I have no idea how to fix this problem. I have a set of lights on a car, and i want them to flicker and then turn off when a player gets near them. I wrote a very simple script so that when the player enters a trigger they would flicker, but the lights flicker even when the player hasnt entered the trigger. please help
public class FlickeringCarLights : MonoBehaviour {
public Light light1;
public Light light2;
public Light light3;
public Light light4;
public float minTime = 0.05f;
public float maxTime = 1.2f;
private float timer;
private bool trigger = false;
void start ()
{
light1 = light1.GetComponent<Light> ();
light2 = light2.GetComponent<Light> ();
light3 = light3.GetComponent<Light> ();
light4 = light4.GetComponent<Light> ();
timer = Random.Range (minTime, maxTime);
}
void Update ()
{
if (trigger = true) {
timer -= Time.deltaTime;
if (timer <= 0) {
light1.enabled = !light1.enabled;
light2.enabled = !light2.enabled;
light3.enabled = !light3.enabled;
light4.enabled = !light4.enabled;
}
}
}
void OnTriggerEnter (Collider other)
{
trigger = true;
}
}
Answer by getyour411 · Sep 05, 2015 at 07:34 AM
start should be Start, so it is not getting called.
No filtering on your OnTrigger, so who knows what's triggering it; add something like
if(other.tag == "Player")
// do stuff
Yes! it works perfectly now! thanks for your help.(Im still a newbie in C#). I should also add that in the if statement I forgot to type == ins$$anonymous$$d of =