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 Nathan Bennett · Nov 17, 2010 at 10:35 AM · toggleflashlight

FlashLight Turning off, but not back on again? (Simple Script Fix?)

Hello me again. all i want to do is turn the "f" button on my keyboard into a flashlight button, so the lights on my Gun turn on and off. but the problem I'm getting, is that they turn off, just not back on again? here's my script:

var hazerdLights1 : Light; var hazerdLights2 : Light; var flashlight : Light;

private var defaultRange1 : float; private var defaultRange2 : float; private var defaultRange3 : float; private var flashlightOff = false;

function Start () { defaultHazerdRange1 = hazerdLights1.range; defaultHazerdRange2 = hazerdLights2.range; defaultFlashlightRange = flashlight.range; }

function Update () { if(Input.GetButtonDown("Torch") && !flashlightOff) { hazerdLights1.range = 0; hazerdLights2.range = 0; flashlight.range = 1; flashlightoff = true; } else if(Input.GetButtonDown("Torch") && flashlightOff) { hazerdLights1.range = defaultRange1; hazerdLights2.range = defaultRange2; flashlight.range = defaultRange3; flashlightoff = false; }

}

I'm still a very big noob with this coding business, but i thought this seems ok. i get no error from the console, yet all i can do, is turn the light's off, not back on again?

any help, like always, is extremely helpful.

Comment
Add comment
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

4 Replies

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

Answer by duck · Nov 17, 2010 at 12:49 PM

Typically you'd switch a light on and off with a script placed directly on the light, negating the need for variable references, like this:

function Update() {
    if (Input.GetButtonDown("Torch")) {
        light.enabled != light.enabled;
    }
}

However, if you want to control two lights, from a script placed on a third object, you need variable references to the light objects (as you had set up), and just control them in a similar way, like this:

var hazardLight1 : Light; var hazardLight2 : Light;

function Update() { if (Input.GetButtonDown("Torch")) { hazardLight1.enabled = !hazardLight1.enabled; hazardLight2.enabled = !hazardLight2.enabled; } }

I'm not entirely clear what you were trying to do with the other light, but using code similar to this you should be able to do whatever you want with it.

The above script toggles both lights, so it kind of assumes you have them starting either both off, or both on. If you have them starting with one off and one on, hitting the Torch button will switch one off and the other on!

Comment
Add comment · Show 3 · 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 Nathan Bennett · Nov 17, 2010 at 02:36 PM 0
Share

Nope...still don't work. comes up with errors "Expressions in statement must be executed for the side effects" or something along the lines.

avatar image duck ♦♦ · Nov 17, 2010 at 02:59 PM 0
Share

oops! I wrote != ins$$anonymous$$d of = ! (Fixed)

avatar image Nathan Bennett · Nov 18, 2010 at 09:08 PM 0
Share

Thankyou very much!

avatar image
1

Answer by oliver-jones · Nov 17, 2010 at 11:29 AM

Humm,

Just try replacing the second (Input.GetKeyDown("f)) with a different Key ... say 'd' and see if that will work (for debugging)

So: F = On D = Off

Also, make sure in your Input (Edit > PrjectSettings > Input) has a define for the key input ... but I don't think that will make a difference to be honest.

Comment
Add comment · 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
-1

Answer by oliver-jones · Nov 17, 2010 at 12:06 PM

If you get it to work, then let me know please

Comment
Add comment · Show 2 · 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 duck ♦♦ · Nov 17, 2010 at 12:50 PM 0
Share

Don't add comments as new answers please.

avatar image oliver-jones · Nov 17, 2010 at 01:03 PM 0
Share

$$anonymous$$y apologies, still kind of new around here

avatar image
0

Answer by oliver-jones · Nov 17, 2010 at 10:45 AM

I have done something like this. I have a flash light on my assault rifle which can be turned on or off by pressing the F key. I have done it slightly cheaper to you. But mine works :)

This is what I do: (I do not have my Unity in front of me, so I can't test it 100%)

var FlashLight : Light; //drag your light object onto here var FlashLightOn = false; var LightToggle = false;

function Start() { FlashLight.enabled = false; }

function Update() { if(Input.GetKeyDown("f") && LightToggle == false) { FlashLight.enabled = true; LightToggle = true; }

//You might want to put an else if -- check tho if(Input.GetKeyDown("f") && LightToggle == true) { FlashLight.enabled = false; LightToggle = false; } }

(again, not tested)

Comment
Add comment · Show 2 · 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 Nathan Bennett · Nov 17, 2010 at 11:12 AM 0
Share

Thanks. i still don't know all the API's of unity yet. Just disabling the light seems a better idea :D

avatar image Nathan Bennett · Nov 17, 2010 at 11:21 AM 0
Share

Still not working. i'll have a little fiddle...

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

No one has followed this question yet.

Related Questions

toggle projector as light (C#) 0 Answers

Flash light toggle problem 1 Answer

help with Flashlight Pickup 1 Answer

How to make a flashlight toggleable in C#? 6 Answers

How to make a Flashlight 5 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