Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TunerSymphon · Nov 25, 2013 at 12:03 AM · inputlightingupdatelight

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image iwaldrop · Nov 25, 2013 at 02:34 AM 1
Share

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;    
             }
         }
     }
 }
 
avatar image TunerSymphon · Nov 25, 2013 at 04:05 AM 0
Share

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!

avatar image iwaldrop · Nov 25, 2013 at 04:27 AM 0
Share

Well accept supernat's answer, since I was just trying to get you to realize what you were missing on your own. ;)

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by supernat · Nov 25, 2013 at 03:19 AM

Add brackets {} to the if/else statement.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TunerSymphon · Nov 25, 2013 at 04:16 PM 0
Share

Got it. Works now, thanks! Can't believe I made such a stupid mistake. XD

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges