error: Ambiguity between 'Battery.Power' and 'Battery.Power'

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. :slight_smile:

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 :wink:

That said, this one just means you have two scripts named “Battery”. Gotta change one of them.

1 Like

okay, thx :slight_smile:

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
  1. Already covered :smile: see my first comment & link
  2. It’s not just 0.1, it’s 0.1 * Time.deltaTime, which is a whole other ball of wax (with the same solution)
1 Like

Oops, didn’t see that mate. Good catch.