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
1
Question by La Frite Chaude · Oct 14, 2010 at 07:24 PM · lightcartoggleswitchintensity

How to turn on a light, then off with the same button.

Hi, I'm trying to turn the headlights on, and off with the same button I started scripting a mounth ago and just can't change this basic thing. So my script is way out off bounds and i'd appreciate help ^^ :

private var lightson = false;

function Update () {

   if(Input.GetButton("headlights")){

       lightson = true;
       light.intensity = 1;

       }

   if(Input.GetButton("headlights")){

       lightson = false;
       light.intensity = 0;

       }

}

Comment
Add comment · Show 1
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 duck ♦♦ · Oct 14, 2010 at 07:32 PM 0
Share

This question is similar to *http://answers.unity3d.com/questions/5270/a-simple-light-switch*, but has a few unique points which aren't covered in that answer which need explaining, so I think it warrants its own answer.

1 Reply

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

Answer by duck · Oct 14, 2010 at 07:28 PM

Simple question with a fairly simple answer, but here's a few subtle points:

  • You don't need to track a separate boolean on/off value. Lights have their own "enabled" boolean property which can be used to switch them on and off.

  • You can "toggle" a boolean by using the "not" operator, like this: myBool = !myBool;

  • You need to use GetButtonDown rather than GetButton, otherwise the light will continue to toggle every frame while the key is held down. GetButtonDown only occurs once, when the key is pressed, then doesn't trigger again until the key is released and pressed again.

So, combining those points into something like this should do the job:

function Update() {
    if(Input.GetButtonDown("headlights")) {
        light.enabled = !light.enabled;
    }
}
Comment
Add comment · Show 4 · 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 La Frite Chaude · Oct 14, 2010 at 07:40 PM 0
Share

Thanks,i almost cried myself to hapiness. You're awesome and i got a lot to learn. One more thing : where do you get those kinda tricks ? The Referance ?

avatar image duck ♦♦ · Oct 14, 2010 at 08:14 PM 1
Share

Those sort of things mainly come from both reading the reference manual, and experience :-)

avatar image duck ♦♦ · Oct 14, 2010 at 08:15 PM 0
Share

p.s. remember to upvote & accept answers here!

avatar image nswayze duck ♦♦ · Dec 18, 2017 at 03:45 AM 0
Share

Thank you @duck this really helped me out as well.

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

1 Person is following this question.

avatar image

Related Questions

Toggle Scripts On & Off 1 Answer

Light.intensity and Lumen comparison 0 Answers

Changing light Intensity though script does nothing 1 Answer

light intensity change with easing problem!! 1 Answer

How to make flary light reflection 1 Answer


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