- Home /
NullReferenceException: Object reference not set to an instance of an object.
I'v been following the BurgZergArcade tutorials and came across this error: NullReferenceException: Object reference not set to an instance of an object
In these Following lines:
eh.AddjustCurrentHealth(-10);
Attack();
I really hope anyone can help cause i'm lost at this. Heres my entire "Player Attack" C# Script:
using UnityEngine; using System.Collections;
public class PlayerAttack : MonoBehaviour { public GameObject target; public float attackTimer; public float coolDown;
// Use this for initialization
void Start () {
attackTimer = 0;
coolDown = 2.0f;
}
// Update is called once per frame
void Update () {
if(attackTimer > 0)
attackTimer -= Time.deltaTime;
if(attackTimer < 0)
attackTimer = 0;
if(Input.GetKeyUp(KeyCode.F)) {
if(attackTimer == 0) {
Attack();
attackTimer = coolDown;
}
}
}
private void Attack() {
float distance = Vector3.Distance(target.transform.position, transform.position);
Vector3 dir = (target.transform.position - transform.position).normalized;
float direction = Vector3.Dot(dir, transform.forward);
if(distance < 2.5f) {
if(direction > 0) {
EnemyHealth eh = (EnemyHealth)target.GetComponent("EnemyHealth");
eh.AddjustCurrentHealth(-10);
}
}
}
}
Answer by aldonaletto · Mar 31, 2013 at 01:08 AM
Probably the script EnemyHealth was not found in the target object, what assigns a null value to eh and causes this error. Verify whether the target has such script, or add a debug line like this:
EnemyHealth eh = (EnemyHealth)target.GetComponent("EnemyHealth");
if (!eh){
print("EnemyHealth not found");
} else {
eh.AddjustCurrentHealth(-10);
}
Hi thx aldonaletto for your advice i'v trieditand as you prodicted it doesnt find the game object.. but how do i make it so??
Answer by whydoidoit · Mar 31, 2013 at 10:28 AM
Make sure that all of the enemies have the EnemyHealth script attached.
Ensure that the Attack function cannot be called with a target that is not equipped with an EnemyHealth
If attack can only target things with EnemyHealth then change the script to indicate that by defining target as EnemyHealth not GameObject
Also don't do this: (EnemyHealth)target.GetComponent("EnemyHealth");
Do this: target.GetComponent< EnemyHealth>()
It's faster, type safe (less error prone) and less typing!
i only have it on a test scene wiith 1 enemy and i has enemy health on it.. i just have no ideer how to make the script find the enemy health, can you give me an example??
Take a screen shot of your Editor window when you have an enemy selected (and the game is running) then post that screen shot by editing your question and another with the player selected
Actually have you set the "target" variable of this attack at all?
yea i set the target to the Enemy gameobject and i'v tried anything that came in my $$anonymous$$d...
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
NullReferenceException 1 Answer
Scripting error comes up when I press play 1 Answer
NullReferenceException 0 Answers
NullReferenceException problem 2 Answers