- Home /
NullReferenceException - Why?
Hello everybody,
I'm a beginner in scripting, but I tried to make a script that allows me to have a flashlight with limited power, an on and off switch and a GUI Label that shows the enery that is left. Unfortunately I'm getting a NullReferenceException and I couldn't find out why. Could anyone please help me?
This is my script:
using UnityEngine;
using System.Collections;
public class Flashlight : MonoBehaviour {
public Light flashlightObject;
public float powerLevel = 500.0f;
void OnGui()
{
float percent;
percent = powerLevel / 5;
GUI.Label(new Rect(10, 10, 100, 20), "Battery: " + percent + "%");
}
// Use this for initialization
void Start () {
flashlightObject.enabled = false;
}
// Update is called once per frame
void Update () {
OnGui();
if(Input.GetKeyDown (KeyCode.F))
{
if(flashlightObject.enabled == false)
{
flashlightObject.enabled = true;
}
else
{
flashlightObject.enabled = false;
}
}
if(flashlightObject.enabled == true)
{
powerLevel -= 0.1f;
if(powerLevel <= 0.1)
{
flashlightObject.enabled = false;
powerLevel = 0.1f;
}
}
}
}
I always get this error: "NullReferenceException: Object reference not set to an instance of an object"
I'm happy for any kind of help.
Thanks in advance,
Frigo1706
When you get this kind of error in the console, click on it and it will take you to the class/line that's causing this, which you should include here. The issue could be your public flashlightobject - - do you have something assigned to that in the Inspector?
Hello getyour411,
The line that's causing the error is: GUI.Label(new Rect(10, 10, 100, 20), "Battery: " + percent + "%");
The problem is that there is no GUI.Label if I start the game. Switching the light on and of is no problem and the limited power is no problem either. I have a spotlight assigned to the public flashlightobject in the inspector, but it doesn't work.
Thank you very much for your help so far.
Frigo 1706
Have a look at my explanation for null reference exceptions, found here.
Hello Jamora,
I read your explaination about NullReferenceExceptions, but I don't know why I get this error. $$anonymous$$y variable "float percent" is never zero, so why do I get this error?
Thanks for your help anyway,
Frigo1706
Answer by aurelioprovedo · Feb 23, 2014 at 04:56 PM
I've uploaded a package with a flashlight system, ready to use. Just download this file, and in Unity go to Assets > Import package > Custom, and choose the downloaded file.
All the imported assets will be at the folder 'Flashlight package'. In there, you can find the prefab called 'linterna', which you will only need to drop in your scene. Then, start the game and click the left mouse button to toggle the flashlight. You will see the battery bar at the left-bottom of the screen. You can specify values for battery life, recharge rate, unusable time after running out of battery...
It should all be quite ready to use, but you can play with the values and tweak them, or modify the code as needed. Oh, and sorry that it's in Spanish, I had no plans on sharing the code when I was doing it.
Note: I did this flashlight for my Ludum Dare 27 gamejam entry. You can play the game here. It's far from clean, it being a quick-made thingy for a gamejam, but it may help you.
Hello aurelioprovedo,
Thank you very much for uploading this package, your flashlight system is exactly what I was looking for. Am I allowed to use it in a game that I'm currently making?
Thanks again,
Frigo1706
Sure, why not! You can add me in the 'Thank you' section of the credits or whatever, if you want. But I won't complain if you don't, it's ok :-)
I'm happy to have helped you, just tell me if you wanna know something else.
Thanks, I will absolutely add you to the credits. And thank you for the offer as well, I might come back to it ;-)
Have a nice evening,
Frigo1706
I'm sorry, but I can't. :-(
"We're sorry, but this cannot be voted up by you until you have at least 15 reputation."
Answer by Linus · Feb 23, 2014 at 03:40 PM
Remove OnGui(); from inside Update
And rename the function to OnGUI()
Hello Linus,
I did so an now I have the GUI.Label when I start the game but when I switch the light on the GUI.Label disappears immeadiately.
Thanks so far,
Frigo1706
Thats another question isnt it, you solved the null reference that this question is about. You now need to work on your flashlight logic. And if you cant get it work withing reasonable time (for me thats 2-4 days) ask for help by the community.
No, that's not really another question. I think that my question hasn't really been answered, because just deleting a line of code doesn't help me at all. I don't just want the NullReferenceException to get away, but I want to have a code that works the way I tried to make it working.
Thanks anyway,
Frigo1706
Yes, I did, but it still doesn't work. Never $$anonymous$$d, I don't need no answer anymore thanks to aurelioprovedo's great package.
Nontheless thank you.
Frigo1706