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
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.
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.
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! :)
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.