Question by
VaygrEmpire · Oct 26, 2016 at 03:39 AM ·
variableclassnull reference exceptioncall
How to access other class? NullReferenceException
What I'm trying to do: call Battery class by setting flashlight and access public variables.
public Battery flashlight;
void Start () {
flashlight = GetComponent<Battery> ();
}
void Update () {
BatteryLife ();
}
void BatteryLife () {
if (flashlight.currentBatteryLife <= 30 && flashlight.currentBatteryLife > 0) { //line #23
doSomething ();
}
}
NullReferenceException: Object reference not set to an instance of an object BatteryAnimation.BatteryLife () (at Assets/Scripts/BatteryAnimation.cs:23) BatteryAnimation.Update () (at Assets/Scripts/BatteryAnimation.cs:18)
If I'm not referencing object, I should be able to see it at script level. But the script level has no indication of errors. currentBatteryLife is public float variable in class Battery. Am I missing something here?
Thanks for help in advance.
Comment
$$anonymous$$ake sure that the Battery class extends the $$anonymous$$onoBehaviour class and there is an instance of the script attached to the same object as this script.