Object reference not set to an instance of an object EnemyBehaviour.Start ()

Hey there I`ve seen some people with the same problem that I’m having… a NullReferenceException, but none of them solved mine…
Here are some bits of information:
//EnemyBehaviour score information:

public int scoreValue = 120;
private ScoreKeeper scoreKeeper;

//where the error occurs

 void Start()
{
    scoreKeeper = GameObject.Find("Score").GetComponent<ScoreKeeper>();
}

//Enemy Death method

private void OnTriggerEnter2D(Collider2D collider)
{
    Projectile missile = collider.gameObject.GetComponent<Projectile>();
    if (missile)
    {
        health -= missile.GetDamage();
        missile.Hit();
        if(health <= 0)
        {
            Destroy(gameObject);
            scoreKeeper.Score(scoreValue);
        }

    }
}

//ScoreKeeper Method

public int score = 0;
public Text myText; 

void Start()
{
    myText = GetComponent<Text>();
    Reset();
}

public void Score(int points)
{
    score += points;
    myText.text = score.ToString();
}

public void Reset()
{
    score = 0;
    myText.text = score.ToString();
}

Prints

Hi @Nocto,

If you can zip up your project and share a link to the file for me I will be happy to take a look for you. The forum will enable you to upload a file that is 10mb or less, so if you project files are large than that please use something like Dropbox or Google Drive etc.

1 Like

Hi @Rob

Somehow after I restarted Unity today the error was gone !
Welp… thanks anyway o/

1 Like

hehe, isn’t that always the way! Well, at least you can move forwards again.

I did look through the code you posted (nicely formatted, thank you) and the screenshots and nothing really jumped out as missing - nice work :slight_smile:

Privacy & Terms