• 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 /
  • Help Room /
avatar image
0
Question by Tomdancer · Apr 17, 2016 at 12:57 PM · nullreferenceexceptionnot working

Any idea why this is throwing a NullReferenceException ?

I'm a little bit stuck on my current project, could use some help. As far as i understand unity's problem is that the hydrationValue variable(at least unity tells me the problem is there...) wich I use to decrease the player.thirst variable is not set. I logged it before the value change takes place and it has the intended value, so I don't really understand the problem. Any idea what i did wrong ?

My code:

 using UnityEngine;
 using System.Collections;
 
 public class Drink : MonoBehaviour {
 
     public float hydrationValue;
     public float alcohol;
     public float amount;
     public float healthEffect;
     public float price;
     public float aggressivityEffect;
     public float calmingEffect;
 
     GameObject player;
 
     public void Sart()
     {
         player = GameObject.FindGameObjectWithTag("Player");
     }
 
     public void drinkWater()
     {
         hydrationValue = 50f;
         alcohol = 0f;
         amount = 1f;
         healthEffect = 5f;
         price = 10f;
         aggressivityEffect = -0.5f;
         calmingEffect = 1f;
 
         player.GetComponent<Homeless>().thirst += hydrationValue;
         player.GetComponent<Homeless>().health += healthEffect;
         player.GetComponent<Homeless>().money -= price;
         player.GetComponent<Homeless>().aggressivity += aggressivityEffect;
         player.GetComponent<Homeless>().calmness += calmingEffect;
         Debug.Log("drinkWater done");
     }   
 
 }
 

Thanks in advance, Tom

Comment
Add comment · Show 2
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 GreatDane · Jan 09, 2017 at 09:44 PM 0
Share

Another trick would have been to put a breakpoint on the line that finds your player. When you attach to Unity and run the game you would have been surprised to find that the breakpoint never gets hit. That might have given you a clue that your method name was misspelled, because Start should get called if the game object is enabled.

The literal answer to your question as to why you are getting an exception is that you have a null value (player) and you are not doing any null checking. Testing for nulls makes for more robust code, and if you log a message to the console when you get nulls you will gave another clue as to why stuff isn't happening the way you expected.

 public void Start()
 {
     player = GameObject.FindGameObjectWithTag("Player");
     if (player == null)
     {
         Debug.LogError("player gameobject was not found");
     }    
 }

and in the method that uses it

     .  .  .
     cal$$anonymous$$gEffect = 1f;

     if (player == null)
     {
         Debug.LogError("player is null");
         return;
     }    

The return statement will stop you from hitting the lines below that would throw exceptions.

avatar image Tomdancer · Jan 09, 2017 at 11:28 PM 0
Share

Thanks for the answer, yeah I didn't have any idea on how to debug stuff properly. It's a bit old question but still thanks for the input.

2 Replies

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

Answer by Cepheid · Apr 17, 2016 at 01:23 PM

Hi there @Tomdancer

It looks like you've made a typo on your Start method so Unity is unable to find the player object which in turn means that it cannot access it's "Homeless" component. Due to it being unable to access this component it cannot increase the "thirst" variable with the "hydrationValue" so it throws an exception error at this point.

Where you've meant to type:

      public void Start()
      {
          player = GameObject.FindGameObjectWithTag("Player");
      }

You've actually typed:

      public void Sart()
      {
          player = GameObject.FindGameObjectWithTag("Player");
      }

I hope this helps! :)

Comment
Add comment · 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
0

Answer by Tomdancer · Apr 17, 2016 at 04:17 PM

Oh my god... I'm embarrassed... I've literally checked for typos like 9001 times before checking anything else. Hah well, I guess I need a trip to the optometry. Thanks for the quick answer, instantly solved the problem.

Comment
Add comment · 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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

57 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Object reference not set to an instance of an Object C# 2 Answers

Unity lists not working while using collection generic 3 Answers

Script that plays animation when player is moving? 0 Answers

Trying to follow tutorial -2D Character Controllers- but it's outdated. Help?,I've been trying to follow the tutorial named - 2D Character Controllers - but I guess it doesn't work anymore 0 Answers

Why my Screen.fullScreen isn't working? 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