- Home /
How do I change a spot light to a point light by pressing a key?
So, right now, I'm attempting to try to get a flashlight to change from a spot light to a point light by pressing "e." I have this code right now:
function Update() {
if (Input.GetKeyDown("e")) {
if (light.type == LightType.Spot)
light.type = LightType.Point;
light.intensity = 3;
light.range = 180;
else
light.type = LightType.Spot;
light.intensity = 5.5;
light.range = 281;
}
}
For some reason, the code doesn't work, but it will work if I get rid of the light.intensity and light.range lines and leave just light.type. It tells me it has errors on line 11 and 19... But when I does what it says to fix it, the light then flashes to point and then goes back to spot immediately. Some help would be greatly appreciated.
I converted your code to C# (for no reason other than I feel more comfortable in it), and ran your code. It worked fine for me.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Light))]
public class LightChanger : $$anonymous$$onoBehaviour
{
void Update()
{
if (Input.Get$$anonymous$$eyDown("e"))
{
if (light.type == LightType.Spot)
{
light.type = LightType.Point;
light.intensity = 3;
light.range = 180;
}
else
{
light.type = LightType.Spot;
light.intensity = 5.5f;
light.range = 281;
}
}
}
}
I tried the code, and it changes the light from spot to point, but the intensity and range values still don't change.
Edit: Turns out I had another code going... And now it's being REALLY weird. I have another code that toggles the flashlight on and off, and whenever I press the key to turn the flashlight, it just switches between point and spot ins$$anonymous$$d of turning off. It seems that your code creates-
Never $$anonymous$$d, just got it to work. Thanks!
Well accept supernat's answer, since I was just trying to get you to realize what you were missing on your own. ;)
Answer by supernat · Nov 25, 2013 at 03:19 AM
Add brackets {} to the if/else statement.
Got it. Works now, thanks! Can't believe I made such a stupid mistake. XD
Your answer
Follow this Question
Related Questions
How do i make night and day transitions 3 Answers
lighting click help 3 Answers
How to fix weird baking noise and lines ? 0 Answers
Add Cursor (Top-Down-Shooter) 1 Answer
Light Looks Weird 2 Answers