Hello, as the title states, I am having an issue. My code is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Battery : MonoBehaviour {
public float Power = 100f;
public Light Flashlight;
//Use this for initialization
void Start () {
Debug.Log("Battery Started Draining");
}
// Update is called once per frame
void FixedUpdate () {
Power -= 0.1f * Time.deltaTime;
if (Power == 0.0f)
{
Flashlight.enabled = !Flashlight.enabled;
}
}
}
Iâm having the error between the two âPowerâ floats being used. This script is used on a Makeshift-Battery comprised of 2 cylinders and a sphere. This is in visual studio 2015, and Unity3D 5.5.0. Thank you for anything you can give to solve this problem.
Can you paste in the full text of the error? Nothing jumps out at me in that script that would cause an error like that.
Though, in line 16, youâll need to change that to <= instead of ==. Thatâs not the cause of this error, but itâll cause your script not to work after you fix this one.
I just tested your script and it doesnât show any errors when i run it.
Is it possible you have a script in your project named âPowerâ?
It changed to Assets/Scripts/Lighting/Battery/Battery.cs(5,14): error CS0101: The namespace global::' already contains a definition for
Batteryâ
My visual studio glitches sometimes, giving me random, errors that fix themselves. 
If VS gives you errors, check for them in Unity first before posting here - the errors donât matter if they arenât in Unity 
That said, this one just means you have two scripts named âBatteryâ. Gotta change one of them.
1 Like
I would like to point out that 0.1 in binary is a repeating number (like how 1/3 is a repeating number in decimal), so itâs heavily prone to float error.
As a result, equality to 0 could potentially fail because you might actually have a value like 0.000000001 or something. Rule of thumb is that you never really test equality of floats.
Also⌠I prefer to use the âthisâ keyword when referencing my fields in a class, it helps in clarifying namespace for the compiler.
1 Like
Oops, didnât see that mate. Good catch.