-1

I am a beginer in C# programming and Unity. When building a 2D game using Unity and dealing with scoring, I encountered a null reference exception. The logic of this part of the game is that when the "Asteroid" collides with "Bullet" it will destroy both objects and will add 1 to the score. Below are part of my codes in both "Asteroid" and "Score" script.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Asteroid : MonoBehaviour
{

    ...

    // For Score Supporting
    Score score;


    ...


    void OnCollisionEnter2D(Collision2D coll)
    {
        if (coll.gameObject.CompareTag("Bullet"))
        {
            score.AddScore(); // Null Reference Exception occured here.
            Destroy(gameObject);
            Destroy(coll.gameObject);
        }
        
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Score : MonoBehaviour
{
    [SerializeField]
    Text scoreText;

    const string scorePrefix = "Score: ";
    int score = 0;

    void Start()
    {
        scoreText.text = scorePrefix + score.ToString();
    }


    public void AddScore()
    {
        score++;
        scoreText.text = scorePrefix + score.ToString();
    }
}

I have attached the "Asteroid" script to the gameobject named "Asteroid" and the "Score" script to a canvas named "Score" and populated "scoreText" in the inspector with the text UI named "ScoreText". After debugging I found that when running the OnCollisionEnter2D function, the score object was actually null. What is the problem with my program and how should I solve it?

  • 3
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Thomas Nov 10 '20 at 07:43
  • Please provide the full error so we can help you – Tom Sebty Nov 10 '20 at 07:43

2 Answers2

0

Just like you did with the scoreText field in the Inspector that you populate with the UI Text object, you also need to associated the Score component with the Asteroid component. Below you'll notice I decorated the Score score; field definition with the [SerializeField] attribute. And just as before, you'll drag the Score component (presumably on a player object, or game manager) into the appropriate field in the Asteroid component.

public class Asteroid : MonoBehaviour
{
    ...
    // For Score Supporting
    [SerializeField]
    Score score;
    ...

    void OnCollisionEnter2D(Collision2D coll)
    {
        if (coll.gameObject.CompareTag("Bullet"))
        {
            score.AddScore(); // Null Reference Exception occured here.
            Destroy(gameObject);
            Destroy(coll.gameObject);
        }        
    }
}

As you code stands, you've declared the score variable as a type of Score, but you haven't yet associated it with an actual instance of a Score object. That's why you're getting the null reference exception.

Milan Egon Votrubec
  • 1,650
  • 1
  • 6
  • 11
-2

Have you populated the Score object on the asteroid script? I believe this is your issue

Tom Sebty
  • 127
  • 8
  • 3
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/27585691) – Re Captcha Nov 10 '20 at 08:13