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!
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.
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
Yeah, but how to do that? Can you write here that part of script right? I realy need that flashlight for my game.
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
Follow this Question
Related Questions
See Light through fog 0 Answers
Light color is equal to Object color/ script? 0 Answers
Disable light not working 0 Answers