- Home /
Flashlight Battery Error No definition
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);
}
}
Are you trying to set the batteryPercentage to batteryPower?
It's looking for a function called batteryPercentage which doesn't exist in your Flashlight class.
If you want to set the public variable you just need something like:
Flashlight.batteryPercentage = batteryPower;
Also, you appear to be trying to access an instance member with the class itself. Unless your variable is static, this isn't what you need. You need to find the Flashlight component on the object.
Flashlight someFlashlight = other.GetComponenet() as Flashlight;
someFlashlight.batterPercentage = whatever;
using UnityEngine;
using System.Collections;
public class Battery : $$anonymous$$onoBehaviour {
//Put this script on the pickup
public int batteryPower = 10; //The amount of battery power to give, negative value hogs energy ins$$anonymous$$d
void OnTriggerEnter ( Collider other ){
if (!other.CompareTag("Player")) return;
Flashlight someFlashlight = other.GetComponenet() as Flashlight;
someFlashlight.batteryPercentage = batteryPower;
Destroy (gameObject);
}
}
now gives me error
Type `UnityEngine.Collider' does not contain a definition for `GetComponenet' and no extension method `GetComponenet' of type `UnityEngine.Collider' could be found (are you missing a using directive or an assembly reference?)
Answer by Ricewind1 · Nov 05, 2014 at 01:00 AM
You need to reference the script you are using first, like below:
Flashlight fLight = other.GetComponent<Flashlight>(); //Assuming that "other" has the flashlight as a script on its gameobject
fLight.batteryPercentage += batteryPower;
$$anonymous$$y new battery code is
using UnityEngine;
using System.Collections;
public class Battery : $$anonymous$$onoBehaviour {
//Put this script on the pickup
public int batteryPower = 10; //The amount of battery power to give, negative value hogs energy ins$$anonymous$$d
void OnTriggerEnter ( Collider Player){
Flashlight fLight = Player.GetComponent<Flashlight>(); //Assu$$anonymous$$g that "other" has the flashlight as a script on its gameobject
fLight.batteryPercentage += batteryPower;
Destroy (gameObject);
}
}
It doesn't give any errors until I put it on a cube , untaged, with a box collider trigger, and when I walk into it, it gives me an error at line 10
NullReferenceException: Object reference not set to an instance of an object Battery.OnTriggerEnter (UnityEngine.Collider Player) (at Assets/Scripts/Flashlight/Battery.cs:10)
Check if it's really the player. Also, is the flashlight a script ON the player?
void OnTriggerEnter ( Collider col){
if(col.tag == "Player")
{
Flashlight fLight = col.GetComponent<Flashlight>(); //Assu$$anonymous$$g that "other" has the flashlight as a script on its gameobject
fLight.batteryPercentage += batteryPower;
Destroy (gameObject);
}
}
I'm testing it now, but I'm using UFPS and it's set up where the flashlight script is on my spotlight itself, which is tagged Player
NullReferenceException: Object reference not set to an instance of an object Battery.OnTriggerEnter (UnityEngine.Collider col) (at Assets/Scripts/Flashlight/Battery.cs:12)
The error I get using your code. The set up is the Spotlight is labeled Player, has the flashlight script, and I put the Battery script on a cube with no tag and a trigger
Your answer
Follow this Question
Related Questions
Recharging battery script, help 1 Answer
Flashlight battery script 3 Answers
Flashlight pickup, battery etc 3 Answers
Pressing "Fire2" Spawns and removes object on click 0 Answers