NullReferenceException: Object reference not set to an instance of an object

Hello!

I’m at lecture 106 “Player Life and death” and I’m struggling a bit, I get the NullReferenceException: Object reference not set to an instance of an object after copying this two methods from the Enemy.cs and pasting them into the Player.cs

private void OnTriggerEnter2D(Collider2D other)
{
    DamageDealer damageDealer = other.gameObject.GetComponent<DamageDealer>();
    ProcessHit(damageDealer);

}

private void ProcessHit(DamageDealer damageDealer)
{
    health -= damageDealer.GetDamage();

    if (health <= 0)

    {

        Destroy(gameObject);


    }
}

and after adding the collider components to the enemy laser and the player, if I try to run the game the very second the enemy shoots the game stops and Unity gives me this error:

NullReferenceException: Object reference not set to an instance of an object
Enemy.ProcessHit (.DamageDealer damageDealer) (at Assets/Scripts/Enemy.cs:75)
Enemy.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/Scripts/Enemy.cs:69)

Here is the entire Enemy.cs code I have

public class Enemy : MonoBehaviour
{

[SerializeField] float health = 100;

[SerializeField] float shotCounter;

[SerializeField] float minTimeBetweenShots = 0.2f;

[SerializeField] float maxTimeBetweenShots = 3F;

[SerializeField] GameObject projectile;
[SerializeField] float projectileSpeed = 10f;



// Use this for initialization
void Start()
{
    shotCounter = Random.Range(minTimeBetweenShots, maxTimeBetweenShots);
}

// Update is called once per frame
void Update()
{
 CountDownAndShoot();


}


private void CountDownAndShoot()

{
    shotCounter -= Time.deltaTime;

    if (shotCounter <= 0f)

    {
        Fire();
        shotCounter = Random.Range(minTimeBetweenShots, maxTimeBetweenShots);
    }
}

private void Fire()

{

    GameObject laser = Instantiate(


        projectile,
        transform.position,
        Quaternion.identity

        ) as GameObject;

    laser.GetComponent<Rigidbody2D>().velocity = new Vector2(0, -projectileSpeed);

}

private void OnTriggerEnter2D(Collider2D other)
{
    DamageDealer damageDealer = other.gameObject.GetComponent<DamageDealer>();
    ProcessHit(damageDealer);

}

private void ProcessHit(DamageDealer damageDealer)
{
    health -= damageDealer.GetDamage();

    if (health <= 0)

    {

        Destroy(gameObject);


    }
}

Does anyone know why do I get the error? I’ve tried to look into it and I just don’t get it. I’ve also checked other NullReferenceException threads but I still don’t get how I can fix this.

Thank you in advance!

Hi Beppe,

NullReferenceException means that a reference (“link”) to an instance is missing. Double click on the error message to see to which line in your code it is referring. If you exposed a field in the Inspector, make sure that it’s not empty.


See also:

Hi Nina,

thank you for the prompt answer! In the console I get the detailed error message:

NullReferenceException: Object reference not set to an instance of an object
Enemy.ProcessHit (.DamageDealer damageDealer) (at Assets/Scripts/Enemy.cs:75)
Enemy.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/Scripts/Enemy.cs:69)

So the script is the Enemy.cs, and the lines are

ProcessHit(damageDealer); // which is part of the OnTriggerEnter2D method
health -= damageDealer.GetDamage(); // which is part of the ProcessHit method

the same methods that can be found here: https://github.com/CompleteUnityDeveloper/06-Laser-Defender/commit/97f6e3717ad9ced5ed914dc5f059405abee416ed

What really puzzles me is that these methods were working until I pasted them into the player script :confused:
Regarding the inspector, I have watched the videos twice and I did the same things that Rick did in the lecture, I don’t know, maybe I’m missing something anyway?
It’s really driving me crazy! It spawns the projectile and then crashes like in this screenshot

If instead I use the Player Laser prefab the game doesn’t crash. :confused:

The only variable of a reference type here is damageDealer. Check where the reference to a DamageDealer object gets assigned to that variable.

Hi Nina,

thank you for this reply, I think it includes the solution to my problem!

I have to admit that in the meantime I found another way to fix it: I duplicated the Player Laser prefab, changed the Sprite and assigned it in the inspector to the enemies!

Thank you so much! Without your help I would still be stuck there :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms