Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by DincaAlin · Nov 28, 2015 at 07:47 PM · c#lighting

Flashlight is not turning off, just on.

I have made my own flashlight version script in c# and when i test it, i can't turn off the flashlight. If I turn off the light manual in edit mode and then i continue playing and press "f" the flashlight is turning on, but then it won't turn off after i press again "f". In my script i have putted a text on screen to know how many battery is remaining and i have added some images with different battery states. When I have the flashlight off, the battery will grow and when it is on the battery will decrease. Here is the script:

using UnityEngine; using System.Collections; using UnityEngine.UI;

public class FlashLight : MonoBehaviour {

 public float battery = 100;
 public bool light = true;
 public Text countText;
 public GameObject full;
 public GameObject optzeci;
 public GameObject patruzecisicinci;
 public GameObject douazeci;
 public GameObject cinci;
 public GameObject descarcat;

 void Start ()
 {
     
 }

 void Update ()
 {
     if(Input.GetButtonDown("Flashlight") & light == true)
     {
         light = false;
     }

     if(Input.GetButtonDown("Flashlight") & light == false & battery >= 1)
     {
         light = true;
     }

     if(light == true)
     {
         this.gameObject.GetComponent<Light>().enabled = true;
     }

     if(light == false)
     {
         this.gameObject.GetComponent<Light>().enabled = false;
     }

     if(light == true)
     {
         battery = battery - Time.deltaTime;
     }

     if(light == false)
     {
         battery = battery + Time.deltaTime;
     }

     if(battery <= 100)
     {
         full.SetActive (true);
         optzeci.SetActive (false);
         patruzecisicinci.SetActive (false);
         douazeci.SetActive (false);
         cinci.SetActive (false);
         descarcat.SetActive (false);
     }

     if(battery <= 80)
     {
         full.SetActive (false);
         optzeci.SetActive (true);
         patruzecisicinci.SetActive (false);
         douazeci.SetActive (false);
         cinci.SetActive (false);
         descarcat.SetActive (false);
     }

     if(battery <= 45)
     {
         full.SetActive (false);
         optzeci.SetActive (false);
         patruzecisicinci.SetActive (true);
         douazeci.SetActive (false);
         cinci.SetActive (false);
         descarcat.SetActive (false);
     }

     if(battery <= 20)
     {
         full.SetActive (false);
         optzeci.SetActive (false);
         patruzecisicinci.SetActive (false);
         douazeci.SetActive (true);
         cinci.SetActive (false);
         descarcat.SetActive (false);
     }

     if(battery <= 5)
     {
         full.SetActive (false);
         optzeci.SetActive (false);
         patruzecisicinci.SetActive (false);
         douazeci.SetActive (false);
         cinci.SetActive (true);
         descarcat.SetActive (false);
     }

     if(battery <= 0)
     {
         full.SetActive (false);
         optzeci.SetActive (false);
         patruzecisicinci.SetActive (false);
         douazeci.SetActive (false);
         cinci.SetActive (false);
         descarcat.SetActive (true);
         light = false;
     }

     SetCountText ();
 }

 void SetCountText ()
 {
     countText.text = battery.ToString();
 }

}

PLEASE HELP ME!

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 mdunstetter · Dec 01, 2015 at 09:53 PM 0
Share

Hi! Yulka's answer seems to be the right answer. Anyway when I look at your code, I see a lot of things that could be simplified. I am not a C# expert, but tell me if you want me to share what I see.

1 Reply

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

Answer by Yulka · Nov 30, 2015 at 02:28 AM

I think it is incorrect condition in Update()

if(Input.GetButtonDown("Flashlight") & light == true) { light = false; } if(Input.GetButtonDown("Flashlight") & light == false & battery >= 1) { light = true; }

You make light false and at next step make it immediatly true. You must union this checks in one if block

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 DincaAlin · Dec 01, 2015 at 11:48 AM 0
Share

Yeah, but how to do that? Can you write here that part of script right? I realy need that flashlight for my game.

avatar image Yulka DincaAlin · Dec 01, 2015 at 09:40 PM 0
Share

Oh, the most simply is to use if...else if construction like this: if(Input.GetButtonDown("Flashlight") & light == true) { light = false; } else if (Input.GetButtonDown("Flashlight") & light == false & battery >= 1) { light = true; }

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

38 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

See Light through fog 0 Answers

Light color is equal to Object color/ script? 0 Answers

Disable light not working 0 Answers

How to change baked lighting when scene loads? 0 Answers

Is it possible to transition LUT over time 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