Object reference not set to an instance of an object

Hello. So I’m working on my first game (still pretty new to C# scripting). I’ve gotten a damage system working when my player comes into contact with an enemy; now I’m trying to get a damage system working to where my player can damage the enemy back, but I’m running into this NullReferenceException.


This is my DamageDealer script which subtracts health from my player (and hopefully eventually my enemy too)

using UnityEngine;
using System.Collections;

public class DamageDealer : MonoBehaviour
{
public float health;//Players health
public GameObject ragdoll;

public void TakeDamage(float dmg)
{
health = health - dmg;

if (health <= 0)
{
Debug.Log(“Die”);//Print out Die to console for troubleshooting
}
}
}

And this is the part of my EnemyController script (which extends DamageDealer) and deals with the collision detection:


void OnTriggerEnter(Collider c)
{
//If player is hit by enemy, deal damage to player
if (c.tag == “Player”) {//If enemy contacts player
canAttackPlayer = true;
animator.SetBool (“Can Attack Player”, true);//Attack by changing canAttackPlayer bool in Animator

//Debug.Log (“Player is hit”);
c.GetComponent ().TakeDamage (10);//Deals 10 damage (Calls TakeDamage method from DamageDealer class)
}

//If enemy is hit by spell, deal damage to enemy
if (c.tag == “Spell”) { //If spell contacts enemy
Debug.Log (“Enemy is hit”);
c.GetComponent ().TakeDamage (10);//Deals 10 damage (Calls TakeDamage method from DamageDealer class)
}
}

The NullReferenceException is occurring when I try to call

c.GetComponent ().TakeDamage (10);

after checking if c.tag == “Spell”.

The exact error message is: “NullReferenceException: Object reference not set to an instance of an object”

Any pointers in the right direction are appreciated!

" if (c.tag == “Spell”) { //If spell contacts enemy
Debug.Log (“Enemy is hit”);
c.GetComponent ().TakeDamage (10);//Deals 10 damage (Calls TakeDamage method from DamageDealer class)
} "

It appears you are trying to make your Spell object take damage? I’m not sure if thats what you intended. Wouldn’t you be calling TakeDamage(10) on the Enemy itself?

Thank you for the reply.

I’m trying to deal damage to my enemy object. by calling the DamageDealer script, but it seems I’m not making the correct reference to it. I was trying to do exactly what I did to successfully deal damage to the player with enemies/ lava/ saws, but I am having a hard time dealing damage to my enemy.

[ code] [ /code] tags when you paste code into the forums please, they really help with with readability, there is a sticky in the scripting section about them.

without knowing too much about how your “spells” work, I’m assuming there are fireballs or something (gameobject with colliders etc.) you appear to be trying to do the “do damage” idea backwards. Handle the collision between the spell/fireball/whatever from the spell end and don’t try to script the interaction from the target.

The first section works because the enemy is attacking the player, the second section doesn’t work because the enemy isn’t supposed to be attacking the spell, the spell is attacking the enemy.

if you really want to continue with the above method, you don’t need the “c.” in the line throwing the error. You are attempting to use a component on the enemy, not on the thing the enemy collided with (i.e. the spell).

Ah, that did it! Thank you so much! Yes, for the moment its just a sphere with a collider, but later on hopefully I’ll be able to swap in something a little more interesting :stuck_out_tongue: It seems to be working by removing the “c” in the beginning of my reference to DamageDealer, but I will probably end up trying to handle the collision from the spell and not the enemy as you suggested. Thank you again for walking me through the logic.

when using things like OnTriggerEnter I tend to call the parameter being passed “other” to remind myself that that is the “other thing we’re colliding with” rather than “c” :slight_smile:

Ah, good tip :slight_smile: Thank you!