Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Frigo1706 · Feb 23, 2014 at 02:23 AM · errorerror messagenullreferenceexception

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

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image getyour411 · Feb 23, 2014 at 02:27 AM 0
Share

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?

avatar image Frigo1706 · Feb 23, 2014 at 03:35 PM 0
Share

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

avatar image Jamora · Feb 23, 2014 at 03:38 PM 0
Share

Have a look at my explanation for null reference exceptions, found here.

avatar image Frigo1706 · Feb 23, 2014 at 05:02 PM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Frigo1706 · Feb 23, 2014 at 05:32 PM 0
Share

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

avatar image aurelioprovedo · Feb 23, 2014 at 06:08 PM 0
Share

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.

avatar image Frigo1706 · Feb 23, 2014 at 06:44 PM 0
Share

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

avatar image aurelioprovedo · Feb 23, 2014 at 07:02 PM 0
Share

PS: how about upvoting my answer? ;-)

avatar image Frigo1706 · Feb 23, 2014 at 09:19 PM 0
Share

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."

avatar image
0

Answer by Linus · Feb 23, 2014 at 03:40 PM

Remove OnGui(); from inside Update

And rename the function to OnGUI()

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Frigo1706 · Feb 23, 2014 at 04:52 PM 0
Share

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

avatar image Linus · Feb 23, 2014 at 05:02 PM 0
Share

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.

avatar image Frigo1706 · Feb 23, 2014 at 05:16 PM 0
Share

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

avatar image Linus · Feb 23, 2014 at 06:18 PM 0
Share

You did also change void OnGui() to OnGUI()?

avatar image Frigo1706 · Feb 23, 2014 at 06:42 PM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

NullReferenceException on empty project because of ScriptCompilerBase 0 Answers

Moving a character to a waypoint 1 Answer

"Assertion failed: Error querying default thread scheduling params: 3" 0 Answers

Unexplanable NullReferenceException Error 2 Answers

im getting this error: NullReferenceException: Object reference not set to an instance of an object Item.Start () (at Assets/Scripts/Inventory System/Item.js:9) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges