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 aoiwef · Aug 27, 2012 at 01:27 AM · c#lightflashlight

Flashlight turning off, but not back on again?

I am using C# to make a spot light attached to the main camera on the firstpersoncontroller turn off and on again with the same key pressed, but it isn't working. I found out that the Update function goes up until the G key is pressed, when the flashlight turns off, but then stops and doesn't start again, not even registering any further G key presses. Could somebody tell me why it isn't working, and how to fix it?

 using UnityEngine;
 using System.Collections;
 
 public class Flashlightcontrol : MonoBehaviour {
 
     public Light Flashlight;
     private bool LightEnabled = true;
    
         
     void Update()
     {
         
         if(Input.GetKey(KeyCode.G))
         {
                  LightEnabled = !LightEnabled;
                  Flashlight.active = LightEnabled;
                         
         }
         
     }
     
 }
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 OperationDogBird · Aug 27, 2012 at 01:36 AM 1
Share

this isnt attached to the light by any chance is it? That would stop it dead cold.

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by TheDarkVoid · Aug 27, 2012 at 01:35 AM

The problem is that if the key is help or down for more than one frame it will continue to turn on and off really quickly. To fix this change:

 if(Input.GetKey(KeyCode.G))

to this:

 if(Input.GetKeyUp(KeyCode.G)) //Triggers for one frame when the key is released.

or this:

 if(Input.GetKeyDown(KeyCode.G)) // Triggers for one frame when the key is pressed.

This will resolve your issue unless something else is turning off your light.

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
0

Answer by aldonaletto · Aug 27, 2012 at 01:36 AM

Change Flashlight.active to Flashlight.enabled:

   Flashlight.enabled = LightEnabled;

active is a GameObject property. If you set Flashlight.active to false, Unity actually does Flashlight.gameObject.active = false, deactivating the game object to which the light belongs, and the scripts attached to it simply stop executing (I bet that the script above is attached to this object...)

EDITED: Also change GetKey to GetKeyDown, as @TheDarkVoid said

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
0

Answer by ravsters · Sep 02, 2012 at 03:34 AM

Here is a working script. I'm using it for a game I'm making. It is in Javascript. Let me know if it helps!

 var lights : Light;
 
 var OnBrightness = 3.0;
 var Flash : float=5.0;
 var Angle : float=60.0;
 var FlashAngle : float=80;
 var FlashDuration : float=0.04;
 var SwitchOnSound:AudioClip;
 var SwitchOffSound:AudioClip;
 
 private var Off : boolean = false;
 private var On : boolean = true;
 private static var lightOff = 0.0;
 
 function flashme (FlashEffect){
     lights.intensity = Flash;
     lights.spotAngle = FlashAngle;
     yield WaitForSeconds (FlashDuration);
     lights.intensity = FlashEffect;
     lights.spotAngle = Angle;
 }
 
 function Update()
 {
     if(Light)
     {
         if(Off)
         {
             if(Input.GetKeyDown(KeyCode.F))
             {
                 flashme(OnBrightness);
                 On = true;
                 Off = false;
                 AudioSource.PlayClipAtPoint(SwitchOnSound, transform.position);
             }
         }
         else
         {
             if(On)
             {
                 if(Input.GetKeyDown(KeyCode.F))
                 {
                     flashme(lightOff);
                     On = false;
                     Off = true;
                     AudioSource.PlayClipAtPoint(SwitchOffSound, transform.position);
                 }
             }
         }
     }
 }
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
0

Answer by ransomink · Mar 26, 2014 at 08:01 AM

Make sure you do not place this script on the light GameObject; any other will do.

 using UnityEngine;
 using System.Collections;
 
 public class ScriptFlashlight : MonoBehaviour
 {
     
     public Light myLight;// the light to be triggered (on and off)
     public bool lightEnabled = true;// if the light is enabled or disabled
     
     // Use this for initialization
     void Start ()
     {
         Debug.Log ( "The light started on!" );
     }
     
     // Update is called once per frame
     void Update ()
     {
         // check if the 'F' key was pressed
         if ( Input.GetKeyDown ( KeyCode.F ) )
         {
             lightEnabled = !lightEnabled;// toggle the boolean value (this will initially be false)
             myLight.enabled = lightEnabled;// enable or disable the light based on the boolean value
             
             // this is just for testing and debug purposes
             // check if the light is enabled
             if ( lightEnabled )
             {
                 Debug.Log ( "Light turned on..." );
             }
             else
             {
                 Debug.Log ( "Light turned off..." );
             }
         }
     }
 }
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

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

12 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Flashlight Flicker! 2 Answers

Distribute terrain in zones 3 Answers

When flashlight hits invisible object, make it appear? 0 Answers

Multiple Cars not working 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