- Home /
Enable scripts on a directional light
Hi there,
I have a script on a directional light to make it flash. What I want to do is to enable that script after a collision. Any help would be greatly appreciated.
Here is my code so far:
var flashinglight: GameObject;
function OnTriggerEnter(trigger: Collider){
flashinglight.light(typeof(LightFlashing)).enabled=true;
}
I am getting this error too:
Assets/Scripts/LightingFlashingEnable.js(4,23): BCE0077: It is not possible to invoke an expression of type 'UnityEngine.Light'.
Answer by kolban · May 30, 2012 at 11:42 PM
A GameObject can have a light attached to it. If it does, that light can be accessed as an Object of type "Light" from the property called "light" on the GameObject. So, in your example:
flashinglight.light
will be an object of type Light. This has a property on it called enabled which defines whether or not the light is enabled or disabled. So to enable the light, one would code:
flashinglight.light.enabled = true;
In your code, you had coded
flashinglight.light(....)....
which basically tried to invoke a method called "light" that would be on the GameObject. However, "light" is a Light object on the GameObject and not a function.
So I would need to replace .light with .(name of script to enable)?
I tried to adapt the code to this but the light starts flashing from when the scene is loaded, no errors thought:
var flashinglight: GameObject;
var trigger: GameObject;
function Awake(){
flashinglight.light.enabled = false;
}
function OnTriggerEnter(trigger: Collider){
flashinglight.light.enabled = true;
}
What does the script look like that you have attached to the light which causes it to flash? I am guessing that there is an Update() method on it that checks what time it is and sets the light on or off accordingly. What I think you want to do is to create an exposed boolean property in your script ... call it "flashingEnabled". In your Update() method, have it check the flashingEnabled boolen. If that is OFF then your Update() should set the light off and return immediately. If it is ON then you are executing your time based flash code. Now, when a collision occurs, you will want to obtain a reference to your script and set its flashingEnabled property to true.
Here is the script I have attached to the light, it is a bit of a mash of different scripts I have found.
var $$anonymous$$Time = .5;
var thresh = .5;
var enable: boolean = false; // boolean flag: enable trigger 1 when true
private var lastTime = 0;
private var myLight;
// function called by trigger2 via Send$$anonymous$$essage:
function EnableTrigger(onOff: boolean){
enable = onOff;
}
function OnTriggerEnter(trigger: Collider){
myLight = GetComponent(Light);
}
function Update (){
if ((Time.time - lastTime) > $$anonymous$$Time)
if (Random.value > thresh)
light.enabled = true;
else
light.enabled = false;
lastTime = Time.time;
}
That is what I was roughly guessing the script would contain. I'm not sure what to offer now. In my last post, I gave the high level recipe as to what would be needed. For me to code it up wouldn't take long but then we'd have to exchange code etc etc. I'm not sure what to suggest. I'm working on my own projects and am happy to assist with short questions ... but time is precious and I can't really code up other folks scripts.
Yes I completely understand! Thank you for what you have done :) I'll try to give it a go and make sense of it. It's more direction than I had before
Answer by whydoidoit · May 31, 2012 at 12:46 AM
I'm presuming that the script in the question is added to the thing that collides with the light?
In which case you need to be getting your script and just turning it on and off, now presuming it might one day collide with other things I suggest it look like this:
function OnTriggerEnter(trigger: Collider){
var flashingLight = trigger.gameObject.GetComponent(LightFlashing);
if(flashingLight != null)
flashingLight.enable = true;
}
I presume your script in the comments is called LightFlashing for this to work, it can be simpler:
var minTime = .5;
var thresh = .5;
var enable: boolean = false; // boolean flag: enable trigger 1 when true
function Start() {
light.enabled = false;//Or true if you want it on to start with
}
private var lastTime = 0;
function Update (){
if(!enable)
return;
if ((Time.time - lastTime) > minTime) {
if (Random.value > thresh)
light.enabled = true;
else
light.enabled = false;
lastTime = Time.time;
}
}
To get this to trigger there must be colliders and one of the objects must have a rigidbody (it can be isKinematic = true)
Now once activated your flashing light is going to keep flashing.... You haven't got code to turn it off there...
@whydoidoit This worked really well thanks!! The only thing is when I replaced my flashing code with what you wrote the light doesn't flash, it only stays on.
I haven't got code to end the flashing as (for now at least) I want it to not stop.
Thanks
I guess you're missing brackets for the if ((Time.time - lastTime) > $$anonymous$$Time)
. Your lastTime variable get set every frame at the moment.
Yeah I see why - I've edited the answer and fixed that. (The lastTime was happening every update - stopping it from every changing! I added { and } to fix it).
I've also added a Start() to disable (or enable) the light when the game runs
hmmm still doesn't flash after the collision...
I have the top code attached to the trigger object and the bottom code attached to the directional light
Your answer
Follow this Question
Related Questions
How to make flashing lights faster? 1 Answer
Flashes of light 0 Answers
Directionnal light flashing 0 Answers
Why isn't my flashing script working? 1 Answer
Rendering sun-shafts even when light is not in view. 0 Answers