• 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 Julian_Spring · Mar 09, 2013 at 12:54 PM · javascripttorch

When pressing F flash does not stay on.

So after I have a flashlight after I make hastorch = true which kinda works but the torch does not stay on. Sorry about the long post. Here is my FlashLight script.

 var lightSource : Light; 
 static var EnergyPerBattery : float = 75;
 static var BatteriesLeft : int = 0; 
 static var turnedOn : boolean = false; 
 private var drainSpeed : float = 0.5;
 static var hastorch = false;
 
  
  
 function Update () {
    if(hastorch == true){
         if (Input.GetKey(KeyCode.F)) ToggleFlashlight();
     }
 }
  
 
 function ToggleFlashlight(){
    if(hastorch == true){
      turnedOn=!turnedOn;
         if(turnedOn && EnergyPerBattery>0){
                TurnOnAndDrainEnergy();
        }
 } 
     else{
        lightSource.enabled = false;
     }
 }
  
 
 function TurnOnAndDrainEnergy () {
         lightSource.enabled = true;
         while (turnedOn && EnergyPerBattery>0) {
            EnergyPerBattery -= drainSpeed*Time.deltaTime;
                yield;
         }
     lightSource.enabled = false;
         
 }
 
 function OnGUI(){
     GUI.Box (Rect (Screen.width - 100,Screen.height - 50,100,50), EnergyPerBattery.ToString());
     
 }
 
 static function AlterEnergy (amount : int){
     
 }

Here is my PickUpTorchScript

 var batteryPower : int = 10;
 
 var hit : RaycastHit;
 var mesh : GameObject;
 var Range : float = 1;
 var LookingAt = false;
 var Destroyed = false;
 
 
 
 function Update (){
 
 
 var fwd = transform.TransformDirection (Vector3.forward);    
     var hit : RaycastHit;
         if(Physics.Raycast (transform.position, fwd, hit, Range)){
         Debug.DrawRay(transform.position, fwd * Range, Color.red);
             if( hit.collider.gameObject.tag == "Torch"){
                 LookingAt = true;
                     if(Input.GetKeyDown(KeyCode.E)){
                         Destroy(mesh);
                             Destroyed = true;
                             LookingAt = false;            
             }
             else if( hit.collider.gameObject.tag == null){
                 LookingAt = false;
             }                
         }
     }
     if(Destroyed == true){
         FlashLight.BatteriesLeft += 1;
         FlashLight.hastorch = true;
     }
     
 
 }
 function OnGUI(){
     if(LookingAt == true){
         GUILayout.BeginArea (Rect((Screen.width/2)-50, (Screen.height/2) , 100, 100));
         GUILayout.Label ("E To Interact");
         GUILayout.EndArea ();        
      }     
 }
Comment
Add comment · Show 2
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 ByteSheep · Mar 09, 2013 at 01:10 PM 0
Share

Try replacing Get$$anonymous$$ey with Get$$anonymous$$eyDown

Get$$anonymous$$ey is called every frame that a key is pressed, while Get$$anonymous$$eyDown is only called on the first frame.

avatar image Julian_Spring · Mar 09, 2013 at 02:29 PM 0
Share

Thanks for the answer, but no that does not work sadly.

1 Reply

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

Answer by dorpeleg · Mar 09, 2013 at 02:48 PM

This is your problem:

 function TurnOnAndDrainEnergy () {
         lightSource.enabled = true;
         while (turnedOn && EnergyPerBattery>0) {
         EnergyPerBattery -= drainSpeed*Time.deltaTime;
             yield;
         }
     lightSource.enabled = false;
  
 }

change it to:

 function TurnOnAndDrainEnergy () {
         while (turnedOn && EnergyPerBattery>0) {
             EnergyPerBattery -= drainSpeed*Time.deltaTime;
             lightSource.enabled = true;
             yield;
         }
         if (EnergyPerBattery <= 0) {
            lightSource.enabled = false;
            turnedOn = false;
         }
  
 }
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 Julian_Spring · Mar 09, 2013 at 04:42 PM 0
Share

Hey thanks

That work but now there is a new problem the Light doesn't switch off when I press F again

Thanks again

avatar image Julian_Spring · Mar 09, 2013 at 04:46 PM 0
Share

Never $$anonymous$$d I got just added

 if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F)){
         lightSource.enabled = false;
     }
 
 

In the ToggleFlashligh function

avatar image dorpeleg · Mar 09, 2013 at 04:55 PM 0
Share

Please mark as answered then :)

avatar image Julian_Spring · Mar 09, 2013 at 05:43 PM 0
Share

Thanks again hey :)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

What is a good unityscript writing program? 1 Answer

Buttonpress animation 1 Answer

How to make an "interaction zone". 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