Sorry to come back so soon, but I tried coding a simple battery script to my flash light and I’m sorta stuck. I usually code java but this amazing flashlight script is in C#, so that’s what I’ve been using. I’m stuck as to how I can fix this error with my two scripts and any help in the right direction is appreciated. Sorry I’m really new to C# so thank you all !
Error
Assets/Scripts/Flashlight/Battery.cs(11,28): error CS0117: Flashlight' does not contain a definition for
batteryPercentage’
Flashlight.cs
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Light), typeof(AudioSource))]
public class Flashlight : MonoBehaviour {
public AudioClip clickSound;
public float batteryLifeInSec = 300f;
public float batteryPercentage = 100;
private bool on;
private float timer;
void Update() {
Light lite = GetComponent<Light>();
timer += Time.deltaTime;
if(Input.GetKeyDown(KeyCode.F) && timer >= 0.3f && batteryPercentage > 0) {
on = !on;
audio.PlayOneShot(clickSound);
timer = 0;
}
if(on) {
lite.enabled = true;
batteryPercentage -= Time.deltaTime * (100 / batteryLifeInSec);
}
else {
lite.enabled = false;
}
batteryPercentage = Mathf.Clamp(batteryPercentage, 0, 100);
if(batteryPercentage == 0) {
lite.intensity = Mathf.Lerp(lite.intensity, 0, Time.deltaTime * 2);
}
if(batteryPercentage > 0 && batteryPercentage < 25) {
lite.intensity = Mathf.Lerp(lite.intensity, 0.5f, Time.deltaTime);
}
if(batteryPercentage > 25 && batteryPercentage < 75) {
lite.intensity = Mathf.Lerp(lite.intensity, 0.8f, Time.deltaTime);
}
if(batteryPercentage > 75 && batteryPercentage <= 100) {
lite.intensity = Mathf.Lerp(lite.intensity, 1, Time.deltaTime);
}
}
}
Battery.cs
using UnityEngine;
using System.Collections;
public class Battery : MonoBehaviour {
//Put this script on the pickup
public int batteryPower = 10; //The amount of battery power to give, negative value hogs energy instead
void OnTriggerEnter ( Collider other ){
if (!other.CompareTag("Player")) return;
Flashlight.batteryPercentage(batteryPower);
Destroy (gameObject);
}
}