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

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! :slight_smile:

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.