- Home /
NullReferenceException
NullReferenceException: Object reference not set to an instance of an object EnemyAttack.Attack () (at Assets/Scripts/EnemyAttack.cs:43) EnemyAttack.Update () (at Assets/Scripts/EnemyAttack.cs:25) After i hit play and the evil cube tries to attack me
basicli i m learning from the berg zerg arcade tutorials and i've been good so far but now i get this error NullReferenceException: Object reference not set to an instance of an object EnemyAttack.Attack () (at Assets/Scripts/EnemyAttack.cs:43) EnemyAttack.Update () (at Assets/Scripts/EnemyAttack.cs:25) After i hit play and the evil cube tries to attack me here is my script please help me
using UnityEngine;
using System.Collections;
public class EnemyAttack : 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(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);
Debug.Log(direction);
if(distance < 2.5f) {
if(direction > 0) {
PlayerHealth ph = (PlayerHealth)target.GetComponent("EnemyHealth");
ph.AddjustCurrentHealth(-10);
}
}
}
}
Answer by iwaldrop · Feb 17, 2013 at 10:41 PM
The line number of the error is telling you that it hasn't found PlayerHealth ph. There probably isn't one on your target. You should build in protections for this kind of thing so that you never get error messages, and so that you can handle cases where a component you're looking for isn't found.
PlayerHealth ph = (PlayerHealth)target.GetComponent("EnemyHealth");
if (ph != null)
ph.AddjustCurrentHealth(-10);
else
// do something if PlayHealth is not found
Also on line 29 be sure to remove that semicolon at the end of the line. Hope that helps...
can you give me an example how to to make it so if it does not find it to try to find it again or something else wich will be helpful btw thank you very much this is my home porject i have a project for school and i m learing much more :) the only way to really remeber something is to get it wrong the first time :) please respond quickly :)
Well that's exactly what I already did up there. The code (above) says that we're going to find a class called 'PlayerHealth' and call it ph. Then we're going to check if ph is not equal to (!=) null, and if it's not then we're going to do something with it. Of course, if it is then we can handle it in the else statement.
Now, of course you could just keep looking for it if we don't find it the first time, but if we haven't found it through the means above then chances are that it doesn't exist on the GameObject, and that something is wrong because, by design, we're going to have it there.
Another way to find objects is to use the singleton pattern, which you can read more about here.
Answer by KojiUnity · Jul 16, 2013 at 02:16 AM
Probably you've already solved your problem. And I was having this same problem just a few minutes ago (using the same tutorials). In my case it was because the script for the class PlayerHealth.cs was not directly on the Player object it was inside Graphics which is inside de Player object I just moved the PlayerHealth.cs script directly to the Player object and the error did no repeat.
Your answer
Follow this Question
Related Questions
Anyone Else wanna take a wack at the question ? Need help fixing a script error... 5 Answers
Object reference not set to an instance of an object? 1 Answer
NullReference Problem with pragma strict 1 Answer
How can i fix this weird error 2 Answers
Need a hand using array.Length in a C# editor script 1 Answer