why do i get the error Object reference not set to an instance of an object?

when i click play i get this error

NullReferenceException: Object reference not set to an instance of an object
GravityScript.Update () (at Assets/Scripts/GravityScript.cs:22)

i am trying to get the score from FoodCollider.cs and use it in GravityScript.cs

FoodCollider.cs

    public float SmallIncrease;
    public float MediumIncrease;
    public float LargeIncrease;
    public float CameraZoom = 0.02f;
    public int Cooldown = 10;
    private float timeStamp;
    public int Mass = 1;
    public int score = 0;
    public string scoreText = "Score: 0";
    public string SpaceDust = "Space Dust";
    public string Asteroid = "Asteroid";
    public string Planet = "Planet";
    public string BlackHole = "BlackHole";

    //void Start()
    //{
    //MovementScript ms = GameObject.FindObjectOfType<MovementScript>();
    //}

    // void Update()
    // {
    //    if (timeStamp <= Time.time)
    // {
    //  timeStamp = Time.time + Cooldown;

    //  transform.localScale -= new Vector3(Increase/ms.Speed, Increase/ms.Speed, Increase/ms.Speed);
    // }
    //}

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "SmallFood")
        {
            //if (other.transform.localScale =< transform.localScale)
            // {

            //}
            transform.localScale += new Vector3(SmallIncrease, SmallIncrease, CameraZoom);
            Destroy(other.gameObject);
            score += 10;
            scoreText = "Score : " + score;
        }
        if (other.gameObject.tag == "MediumFood")
        {
            transform.localScale += new Vector3(MediumIncrease, MediumIncrease, CameraZoom);
            Destroy(other.gameObject);
            score += 25;
            scoreText = "Score : " + score;
        }
        if (other.gameObject.tag == "LargeFood")
        {
            transform.localScale += new Vector3(LargeIncrease, LargeIncrease, CameraZoom);
            Destroy(other.gameObject);
            score += 60;
            scoreText = "Score : " + score;
        }
       // if (score >= 100)
       // {
          //  GUI.Box(new Rect(20, 10, 100, 20), SpaceDust);
       // }
       // if (score >= 300)
       // {
        //    GUI.Box(new Rect(20, 10, 100, 20), Asteroid);
      //  }
       // if (score >= 2500)
       // {
       //     GUI.Box(new Rect(20, 10, 100, 20), Planet);
      //  }
      //  if (score <= 2500)
      //  {
       //     GUI.Box(new Rect(20, 10, 100, 20), BlackHole);
       // }

    }
    void OnGUI()
    {
        GUI.Box(new Rect(10, 10, 100, 20), scoreText);
    }
}

GravityScript.cs

    private float maxGravDist = 5.0f;
    private GameObject[] planets;
    private float maxGravity = 10.0f;
    private Rigidbody rb;
    FoodColider scoreboard;
    public int score;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
        planets = GameObject.FindGameObjectsWithTag("Planet");
        scoreboard = GetComponent<FoodColider>();
    }

    void Update()
    {
        score = scoreboard.score;

        maxGravDist = 3 * (score / 1000);
    }

    void FixedUpdate()
    {

        foreach (GameObject planet in planets)
        {
            float dist = Vector3.Distance(planet.transform.position, transform.position);
            if (dist <= maxGravDist)
            {
                Vector3 v = planet.transform.position - transform.position;
                rb.AddForce(v.normalized * (1.0f - dist / maxGravDist) * maxGravity);
            }
        }
    }
}

Any help would be appreciated :slight_smile:

You spelled FoodCollider wrong when you initialized the scoreboard.

scoreboard = GetComponent<FoodColider>();

rb = GetComponent();
planets = GameObject.FindGameObjectsWithTag(“Planet”);
scoreboard = GetComponent();

It is possible that the gameobject does not have a Rigidbody, or FoodColider component, in which case this function will return null.

But, in your FixedUpdate you used the values, without checking them first.
If you attempt to use (access any member of) a reference that is null; you will generate that Null Reference Exception.

So, you should check to ensure that you have a valid object reference, before using it…

e.g.

if(rb!=null)
{
  rb.AdForce(...)
}

It is still giving me the error i have looked and everything has a ridgidbody this is my code now.`

@Glurth

public float maxGravDist = 5.0f;
    private GameObject[] planets;
    public float maxGravity = 10.0f;
    private Rigidbody rb;
    FoodColider scoreboard;
    public int score;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
        planets = GameObject.FindGameObjectsWithTag("Planet");
        scoreboard = GetComponent<FoodColider>();
        score = scoreboard.score;
        maxGravDist = 3 * (score / 1000);

        if (rb != null)
        {
            Debug.Log("error");
        }
    }

    void Update()
    {
        score = scoreboard.score;

        maxGravDist = 3 * (score / 1000);
    }

    void FixedUpdate()
    {

        foreach (GameObject planet in planets)
        {
            float dist = Vector3.Distance(planet.transform.position, transform.position);
            if (dist <= maxGravDist)
            {
                Vector3 v = planet.transform.position - transform.position;
                rb.AddForce(v.normalized * (1.0f - dist / maxGravDist) * maxGravity);
            }
        }
    }
}

 public float SmallIncrease;
    public float MediumIncrease;
    public float LargeIncrease;
    public float CameraZoom = 0.02f;
    public int Cooldown = 10;
    private float timeStamp;
    public int Mass = 1;
    public int score = 1;
    public string scoreText = "Score: 0";
    public string SpaceDust = "Space Dust";
    public string Asteroid = "Asteroid";
    public string Planet = "Planet";
    public string BlackHole = "BlackHole";

    //void Start()
    //{
    //MovementScript ms = GameObject.FindObjectOfType<MovementScript>();
    //}

    // void Update()
    // {
    //    if (timeStamp <= Time.time)
    // {
    //  timeStamp = Time.time + Cooldown;

    //  transform.localScale -= new Vector3(Increase/ms.Speed, Increase/ms.Speed, Increase/ms.Speed);
    // }
    //}


    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "SmallFood")
        {
            //if (other.transform.localScale =< transform.localScale)
            // {

            //}
            transform.localScale += new Vector3(SmallIncrease, SmallIncrease, CameraZoom);
            Destroy(other.gameObject);
            score += 10;
            scoreText = "Score : " + score;
        }
        if (other.gameObject.tag == "MediumFood")
        {
            transform.localScale += new Vector3(MediumIncrease, MediumIncrease, CameraZoom);
            Destroy(other.gameObject);
            score += 25;
            scoreText = "Score : " + score;
        }
        if (other.gameObject.tag == "LargeFood")
        {
            transform.localScale += new Vector3(LargeIncrease, LargeIncrease, CameraZoom);
            Destroy(other.gameObject);
            score += 60;
            scoreText = "Score : " + score;
        }
       // if (score >= 100)
       // {
          //  GUI.Box(new Rect(20, 10, 100, 20), SpaceDust);
       // }
       // if (score >= 300)
       // {
        //    GUI.Box(new Rect(20, 10, 100, 20), Asteroid);
      //  }
       // if (score >= 2500)
       // {
       //     GUI.Box(new Rect(20, 10, 100, 20), Planet);
      //  }
      //  if (score <= 2500)
      //  {
       //     GUI.Box(new Rect(20, 10, 100, 20), BlackHole);
       // }

    }
    void OnGUI()
    {
        GUI.Box(new Rect(10, 10, 100, 20), scoreText);
    }
}