I’m trying to find a way to create a script for my crosswalk lights (pretty much like those found in the UK: yellow globes in poles that turn on/off repeatedly).
I know that I will need a triggered time event to do so, but found no way of doing so.
I will probably need something like this:
var lightObject : Light;
function Update() {
everyOneSecond(){ //not a real function :)
lightObject.enabled = !lightObject.enabled;
}
}
My question is: how do I create that everyOneSecond() function?
This is probably something very lame to ask, but I have searched prettymuch everywhere and cannot find a reference to this.
Also, if someone would enlighten me on how to create flickering lights, like the ones you would get in a horror movie (could be something like the above example, only with random time intervals being generated for on/off).
var Mylight: Light;
function Start(){
while(true){
//sets up a loop
MyLight.enabled=false;
yield WaitForSeconds(Random.Range(1,8));
MyLight.enabled=true;
yield WaitForSeconds(Random.Range(1,8));
}
}
Change the random range lines to
yield WaitForSeconds(2.0);
Many thanks AaronC, especially for the prompt response.
Modified it a bit to match my nees and it works just fine - for both situations.
Another thing that I justnoticed: I have a render component halo, attached to the blinking light, that I would like to turn on/off accordingly. Can’t find a way to access this component from the object Get.Component doesn’t seem to work for the halo component.
If you were using what I gave you, which goes on a seperate object to the actual light, (like an empty gameobject- a “light manager” so to speak) you could use
It is not the Draw Halo option on the light itself, but the component added through Component->Rendering->Halo. This one is always active, even if the light is not.
var myLight: GameObject;
function Start(){
while(true){
//sets up a loop
myLight.active=false;
yield WaitForSeconds(Random.Range(1,8));
myLight.active=true;
yield WaitForSeconds(Random.Range(1,8));
}
}
Doesn’t work apparently I cannot make use of the activate property on these game objects. The only way I have been able to make this work was:
var lightpost : Light;
function Start(){
while (true){
lightpost.enabled = !lightpost.enabled;
}
}
Meaning that only the object component is being handled. However, when I change that to a GameObject either modifying the above code or simply by copy/pasting yours it just won’t work…
In your example you have an empty GameObject where you insert the script and associate it with the Point light.
Why?
I thought that, since Light is a specialization of the GameObject class, I only needed to add the script to the Light object and associate the light to the variable. Now I understand that I also need to create an empty GameObject and add the scrit to it… I cannot make sense out of this.
If you were so kind to explain it to me I would be most thankful.
And, above all, a B I G Thank-You for sparing some time to creating this scene for me.
Well in my time of working with Unity, I’ve found it really powerful when you use one object to control another. My EmptyGameObject is what I’d call a “manager” that can take care of other things in the scene. The manager could be a great big cube, with collider set to trigger, with
function OnTriggerEnter(){
MyLight.active=true;
}
on it for example, so the whole thing is switched off until you get near it.
Managers are good, in this occasion I’ve sped up the development cycle by using one. Its just so easy to say
otherObject.active=true;
I mean its not for everyone, and perhaps not everything, but I like it. If you wanted to prefab this as one object you could put the light as a child of the manager, and drag the parent onto an empty prefab. You could also have two scripts on the manager referring to two lights.