Enemy HP & Attack Script Error - Beginner's level

Hello everyone, could someone help me with this? Can’t find the solution. I’m trying to hit the enemy with melee weapon, but every time I hit it Error occurs… It’s health doesn’t go down and there’s an error says " NullReferenceException: Object reference not set to an instance of an object"…

These are the codes, the first one is in AttackScript- - -

var hitpoints : int = 10;
var totarget : float;
var range : float = 5;

function Update () {
if (Input.GetButtonDown(“Attack”)) {
var hit : RaycastHit;
if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit)) {
totarget = hit.distance;
if (totarget < range) {
hit.transform.SendMessage(“DeductPoints”, hitpoints, SendMessageOptions.DontRequireReciever);
}
}
}
}

And here is the second one, in Enemy Script —

var enemyhealth : int = 50;

function DeductPoints (hitpoints : int) {

enemyhealth -= hitpoints;
}

function Update () {
if (enemyhealth <= 0) {
Destroy(gameObject);
}
}

And when I click on the Error message, it says - AttackScript.Update () (at Assets/AttackScript.js:11)

I repeated the process, but same problem occurs, really don’t know what to do. If anyone can please help?

Nobody knows this? lol

Seriously guys, no one knows this? It’s a beginners level problem and I really can’t find a solution. Tried everything again from the start and the same problem appears…

Not sure bud not something I’ve ever used, have you tried posting in the scripting section get a larger audience?

ill have a go and try it myself to get the thread rolling :slight_smile:

I’ve created a couple of default cubes in a test scene, one with the AttackScript and one for the EnemyScript.
And ive positioned the attacker so its Z axis looks at the enemy cube.

Ive copied the scripts you posted, heres what ive used;

#pragma strict

//AttackScript.js


var hitpoints : int = 10;
var totarget : float;
var range : float = 5;

function Update () {
    if (Input.GetButtonDown("Fire1")) {
    var hit : RaycastHit;
        if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit)) {
        totarget = hit.distance;
            if (totarget < range) {
            hit.transform.SendMessage("DeductPoints", hitpoints, SendMessageOptions.DontRequireReceiver);
            }
        }
    }
}

and heres what I have for the EnemyScript

#pragma strict

// EnemyScript.js

var enemyhealth : int = 50;

function DeductPoints (hitpoints : int) {
    enemyhealth -= hitpoints;
}

function Update () {
    if (enemyhealth <= 0) {
    Destroy(gameObject);
    }
}

wondering in the SendMessageOptions of your attackerScript, should it be DontRequireReceiver as opposed to DontRequireReciever, showing a typo error when i copied over your script, but mono did highlight this, so i changed it over.

Seems to work ok for me, so not really sure.

1 Like

Thanks man your post saved me a ton of time. Respect++

For all future reference, posting in the forum is NOT helpful to fix null reference errors.

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Steps to success:

  • Identify what is null <---- START HERE!
  • Identify why it is null
  • Fix that