Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 blackuethel · May 31, 2015 at 08:44 PM · toggleflashlight

How to make a flashlight toggleable in C#?

I haven't been able to find any working scripts/tutorials on this. I just need to know how to turn off a flashlight with the f key.

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 DanGoldstein91 · May 31, 2015 at 11:18 PM 0
Share

Can you post your flashlight code?

avatar image KodyM · Jun 01, 2015 at 12:54 AM 0
Share

Yes, more information is needed.

6 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Dibbie · Jun 01, 2015 at 01:37 AM

You could use a input function. Im just assuming your "flashlight" is actually a directional light in Unity.

 JAVASCRIPT:
 
 var flashlight : Light;
 
 function Update () {
 if(input.GetKeyDown(KeyCode.F) && flashlight.enabled == false){
 flashlight.enabled = true;
 }else if(input.GetKeyDown(KeyCode.F) && flashlight.enabled == true){
 flashlight.enabled = false;
 }
 }
 
 C#:
 
 public Light flashlight;
 
 void Update () {
 if(input.GetKeyDown(KeyCode.F) && flashlight.enabled == false){
 flashlight.enabled = true;
 }else if(input.GetKeyDown(KeyCode.F) && flashlight.enabled == true){
 flashlight.enabled = false;
 }
 }

That should work for you.

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 Chasing_Wyatt · Jun 10, 2016 at 04:33 PM

Here's a flashlight script I use:

using UnityEngine; using System.Collections;

public class Flashlight : MonoBehaviour {

 //Flashlight Object
 public Light flashlight;

 //Flashlight power variables
 public float power = 100.0f;
 private float maxPower = 100.0f;
 private float minPower = 0.0f;

 //Battery charge variable
 private float batteryCharge = 100.0f;

 //Number of batteries currently possesed by the player
 public int batteryCount = 3;

 //Power Drain controls how fast the battery life decreases
 public float powerDrain = 1.0f;

 //Boolean that tells whether or not the flashlight is able to be used based off of current power
 private bool usable = true;

 void Start ()
 {

 }

 void Update () 
 {
     //If the F key is pressed and the power is greater than zero, then the flashlight will toggle between on and off
     if (Input.GetKeyDown (KeyCode.F) && usable) 
     {
         flashlight.enabled = !flashlight.enabled;
     }

     //While the flashlight is off, the power will drain
     if (flashlight.enabled) 
     {
         power -= Time.deltaTime * powerDrain;
     }

     //This is to ensure that the power will never go over 100
     if (power > maxPower) 
     {
         power = maxPower;
     }

     //This is to disable to flashlight and make sure it can't be used until the player uses a battery to recharge the flashlight
     if (power < minPower) 
     {
         power = minPower;
         flashlight.enabled = false;
         usable = false;
     }

     //After you replace the batteries, it allows you to use the flashlight again
     if (power > minPower)
     {
         usable = true;
     }

     //This says that if the player has at least one battery, and if they press R, then the flashlight will be fully charged
     if (Input.GetKeyDown (KeyCode.R) && batteryCount > 0)
     {
         power += batteryCharge;
         batteryCount -= 1;
     }
 }

}

And here's the battery script I use:

using UnityEngine; using System.Collections;

public class Battery : MonoBehaviour {

 //Battery game object
 public GameObject battery; 

 //Boolean that tells if the player is in the battery's collider
 private bool trig = false;

 //When player enters the collider, player will be able to collect the battery
 void OnTriggerEnter ()
 {
     
         trig = true;

 }

 //When player exits the collider, player will be not able to collect the battery
 void OnTriggerExit ()
 {

         trig = false;

 }
     
 void Start () 
 {
     trig = false;
 }
 

 void Update () 
 {
     //When the player enters the trigger and presses the E key, they will recieve a new battery and the battery they collected will be removed from the scene
     if (trig && Input.GetKeyDown (KeyCode.E)) 
     {
         //Finds and acesses the flashlight script
         Flashlight flashlight = GameObject.FindGameObjectWithTag("Flashlight").gameObject.GetComponent<Flashlight> ();

         //Adds a battery
         flashlight.batteryCount += 1;

         //Removes the battery collected from the scene
         Destroy (battery);
     }
 }

}

An important thing to note: You'll need to tag your flashlight as "Flashlight" so that the battery script can access the batteryCount variable.

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 chesterhilly · Jul 24, 2016 at 02:21 PM

@blackuethel Here is a simple way to turn a light on and off by Clicking

 using UnityEngine;
 using System.Collections;
 
 public class Flashlight : MonoBehaviour {
 
     public Light light;    //assign gameobject with light component attached
 
     void Update () {
         if (Input.GetMouseButtonDown (0)) {      //Left mouse button
             light.enabled = !light.enabled;      //changes light on/off
         }
     }
 }

  • Make sure you add the script to GameObject with the light component.

  • Then assign the same GameObject to '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 hananaja132 · Oct 18, 2017 at 05:39 AM

how to make toogleable flashlight button in unity,can someone give me the script please...

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 JohnnyRandom1212 · Nov 04, 2018 at 07:51 PM

um hi why does my batterycount keep on dropping below 0 and nonstop i followed ure code as is, can some one help plz

Comment
Add comment · Show 1 · 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 JohnnyRandom1212 · Nov 04, 2018 at 07:52 PM 0
Share

Here is mycode using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Holo_Cube : $$anonymous$$onoBehaviour { //Flashlight Object public Light holoLight;

 //Flashlight Power variables
 public float holoBattery = 100.0f;
 private float maxPower = 100.0f;
 private float $$anonymous$$Power = 0.0f;

 //Battery Charge variable
 private float batteryCharge = 100.0f;

 //Numberof batteries currently possesed by the player
 public int batteryCount = 3;


 //Power Drain controls how fast the battery life decreases
 public float powerDrain = 1.0f;

 //Boolean that tells whether or not the flashlight is able to be used based off of current power
 private bool usable = true;


 void Update()
 {
     //If the F key is pressed and the power is greater than zero, then the flashlight will toggle between onand off
     if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.F) && usable) 
     {
         holoLight.enabled = !holoLight.enabled;
     }
     //While the Flashlight is on, the power will drain
     if (holoLight.enabled)
     {
         holoBattery -= Time.deltaTime * powerDrain;
     }
     //THis is to ensure that the power will never go over 100
     if (holoBattery > maxPower) 
     {
         holoBattery = maxPower;
     }

     //This is to disable the flashlight and make sure it cant be used until the player uses a battery to recharge the flashlight
     if (powerDrain < $$anonymous$$Power) 
     {

         holoBattery = $$anonymous$$Power;
         holoLight.enabled = false;
         usable = false;
     }

     //After you replace the batteries, it allows you to usethe flashlight again
     if (holoBattery > $$anonymous$$Power) 
     {
         holoBattery += batteryCharge;
         batteryCount -= 1;
     }
     //Checks for batteries below 0
     //if (batteryCount > 0)
     //{
     //    batteryCount = 0;
     //}
 }

}

  • 1
  • 2
  • ›

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

10 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

Related Questions

Flash light toggle problem 1 Answer

help with Flashlight Pickup 1 Answer

How to make a Flashlight 5 Answers

How to make a Flashlight 0 Answers

FlashLight Turning off, but not back on again? (Simple Script Fix?) 4 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