• 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 TheYoyii · Sep 15, 2013 at 12:02 AM · function updatespot lightno lightingpixel lightlight cookie

Flashlight GUI and battery pick up.

Hello !

Im working on a game in the dark where you need battery for your flashlight. T$$anonymous$$s is the script for the flashlight.

 var Flashlight : Light; //Connect the light source in the Inspector
 var LightOn : boolean = true;
 var LightDrainSpeed : float = 0.1;
 var BatteryLife : float = 0.0;
 var MaxBatteryLife : float = 1.5;
 
 function Update () {
     if (LightOn && BatteryLife >= 0)
 {
 BatteryLife -= LightDrainSpeed * Time.deltaTime;
 }
 Flashlight.light.intensity = BatteryLife;
 
 if(BatteryLife <= 0)
 {
 Batterylife = 0;
 }
 
 
 if(Input.GetKeyUp(KeyCode.F))
 {
 if(LightOn)
 {
 LightOn = false;
 }
 else if(!LightOn && BatteryLife >= 0)
 {
 LightOn = true;
 }
 
 Lighten();
 
 }
 }
 
 function Lighten(){
 
 if(LightOn)
 {
 Flashlight.light.active = true;
 }
 else
 {
 Flashlight.light.active = false;
 }
 }

I want to make a GUI to show how many percent left to the battery and i want to make a gameObject Battery when you press E the battery life become 100 %. Please and Thank you.

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

1 Reply

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

Answer by aldonaletto · Sep 15, 2013 at 02:14 AM

Add t$$anonymous$$s code to your script: it displays the charge in percentage with the GUI system, and also has a function to recharge when the battery is picked up:

 function OnGUI(){
   var charge = 100 * BatteryLife/MaxBatteryLife;
   GUI.Label(Rect(20,20,100,40), charge.ToString("F0")+"%"); 
 }

 function ChargeBattery(){ // function called by battery pickup
   BatteryLife = MaxBatteryLife; // sets full charge
 }

The battery pick up must have a trigger: when the player enters the trigger, its script verifies the key E - if pressed, the pickup charges the flashlight and suicides. The pickup script could be somet$$anonymous$$ng like t$$anonymous$$s:

EDITED: Oops! My bad: the event should be OnTriggerStay!

 function OnTriggerStay(other: Collider){
   if (other.tag == "Player" && Input.GetKeyDown("e")){
     // call the function ChargeBattery in the player or its c$$anonymous$$ldren:
     other.BroadcastMessage("ChargeBattery");
     Destroy(gameObject); // destroy the pickup
   }
 }

NOTE: The flashlight script must be attached to the player or to one of its c$$anonymous$$ldren in order to be called by BroadcastMessage.

Comment
Add comment · Show 7 · 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 TheYoyii · Sep 15, 2013 at 06:19 AM 0
Share

Thank You ! but for the pick up i put the script on my battery (GameObject) and when i press E in game nothing happen.

avatar image aldonaletto · Sep 15, 2013 at 05:56 PM 0
Share

This depends on the pickup hierarchy: if the pickup item doesn't have a rigidbody, create an empty gameobject, attach this script to it, add a sphere collider and set its Is Trigger field, then child the battery model to the trigger.

avatar image TheYoyii · Sep 15, 2013 at 11:03 PM 0
Share

Ok but now i got this error and i try to fix it put without success

UnityException: Input Key named: E is unknown

avatar image aldonaletto · Sep 16, 2013 at 01:20 AM 0
Share

Oops! It should be "e" instead of "E". Answer fixed.

avatar image TheYoyii · Sep 16, 2013 at 09:09 PM 0
Share

I dont have a error but its dont work. This is wath i have do: Create a empty gamObject, put a sphere collider and ajust the range. Check Trigger and put the script on it. Wath did i do wrong ?

Show more comments

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

16 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

Related Questions

Spotlight is not working 4 Answers

How do I make a camera flash when I click? 0 Answers

Can a lights falloff be removed? 1 Answer

Shuriken light problem 0 Answers

Which setting dictates how far you can be before a light turns off? 0 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