- Home /
flashlight on/off
how do i turn on and off a pointlight i made the torch and everything and i cant figure out how to access this thankyou btw i prefer C# but dont mind if u cant
Answer by skovacs1 · Nov 01, 2010 at 02:03 PM
Two simple approaches are that you could enable/disable the light or zero/un-zero the intensity:
//Somewhere Light bar = foo.GetComponent<Light>();
//Somewhere later, switch the light on/off if(bar) bar.enabled = !bar.enabled;
or
//Somewhere Light bar = foo.GetComponent<Light>(); float oldIntensity = 0.0f;
//Somewhere later, switch the light on/off float temp = bar.intensity; bar.intensity = oldIntensity; oldIntensity = temp;
i get script errors is this java script or somthing or do i need to add buttons to it sorry i just keep getting script errors saying name 'foo' does not exist in the current context
This is valid c#/mono. foo is an example variable for your flashlight GameObject which you would have to create, find and store however you prefer. If this script is in a class+function attached to your light source, then you can just remove foo and call GetComponent directly. Because you didn't provide any code or context for how or where you were using this script, there is no way to know what you have setup in your code or not.
oh i get ya ill give it a go do i need to add a button input to perform this function?
The "Somewhere" part only implies that the variable oldIntensity be declared outside the function and that the variable bar should also be declared outside the function so that you aren't calling GetComponent every frame - in which case the GetComponent call would be in Start(). The rest of this code you could put into a function of your own and call it based on animation event, button press from the input manager, button press from the GUI or even after a certain amount of time or you could do it in Update() under many of the same conditions - That's up to you.
I'm not really sure why you would ask if you needed to add a button to perform the function. This is simply code that you can call however you like. It needs to be within a function within a class somewhere, but what you do with it and when is up to you. This leads me to believe that you might have some misunderstandings about Unity scripting. If that is the case, I would suggest you look through the tutorials and documentation for a bit because simply accessing a point light could be the least of your worries if you don't know what you're doing.
Your answer
Follow this Question
Related Questions
Low shadow quality from distance light 2 Answers
Custom shader light blending 0 Answers
Terrain Shading 1 Answer
point light gets brighter and darker as it moves 0 Answers
Lighting problem and glitch. 4 Answers