- Home /
Area of effect
Hey! I am working on a tower defence kind of game and I am trying to make projectiles deal damage in an area. When the projectiles hit the targets I get: "Object reference not set to an instance of an object". Here is my script:
public var rad : float = 10;
private var center : Vector3;
private var hitTarget : GameObject;
public var damage : float = 10;
function Update () {
}
function OnCollisionEnter(hitTarget : UnityEngine.Collision){
if(hitTarget.gameObject.tag == "Enemy"){
var objectHit = hitTarget.gameObject;
//Callhealth = GameObject.Find(objectHit).GetComponent(Health);
objectHit.GetComponent(Health).applyDMG(damage);
areaOfEffect(transform.position, 10);
//objectHit.SendMessage("applyDMG", 10);
//Callhealth.applyDMG(damage);
Destroy(this.gameObject);
}
}
function areaOfEffect(center : Vector3, rad : float){
var hitEnemies : Collider[] = Physics.OverlapSphere(center, rad); //från mittpunkten och rad:s värde utåt
for(var x=0; x < hitEnemies.Length; x++){
hitEnemies[x].GetComponent(Health).applyDMG(damage);
}
}
Any kind of help would be much appreciated! :) Thanks in advance.
It is helpful to know what object you're getting the null reference exception on. Can you post the whole error message that Unity generates?
But, I think it's probably the Health component, so I'll take a stab at an answer.
Answer by iwaldrop · Apr 15, 2013 at 05:55 PM
You need to do a check to make sure that your Health Component is actually attached to the object that you've found; the ground, for instance, shouldn't have one...right? :)
function OnCollisionEnter(hitTarget : UnityEngine.Collision)
{
if(hitTarget.gameObject.tag == "Enemy")
{
var objectHit = hitTarget.gameObject;
//Callhealth = GameObject.Find(objectHit).GetComponent(Health);
var health = objectHit.GetComponent(Health);
if (health != null)
{
health.applyDMG(damage);
areaOfEffect(transform.position, 10);
//objectHit.SendMessage("applyDMG", 10);
//Callhealth.applyDMG(damage);
Destroy(this.gameObject);
}
}
}
Aha! Though I am still getting the same error message in the areaOfEffect function at the line: hitEnemies[x].GetComponent(Health).applyD$$anonymous$$G(damage); it must somehow also get ground as hit target it seems. Got any ideas on how to solve that? Thanks!
Well, you're going to have to implement a check for Health being null any time you're grabbing random gameObjets and then trying to do something with Health.
Always create a local reference so that you can check that the type you're looking for is present.
SomeScript s = GetComponent(typeof(SomeScript));
if (s != null)
{
// Do something to s.
}
This works, atleast for the moment:
function areaOfEffect(center : Vector3, rad : float){
var hitEnemies : Collider[] = Physics.OverlapSphere(center, rad); //från mittpunkten och rad:s värde utåt
for(var x=0; x < hitEnemies.Length; x++){
if(hitEnemies[x].gameObject.tag == "Enemy"){
hitEnemies[x].GetComponent(Health).applyD$$anonymous$$G(damage);
}
}
}
Do you think this could cause problems at some point or should it work perfectly?
Your answer
Follow this Question
Related Questions
overlapsphere to destroy NPCs on exit 1 Answer
OverlapSphere seems to detect everything and ignores tags. How can I fix this? 1 Answer
OverlapSphere returned collider arrays 1 Answer
Objects overlapping at origin (0,0,0) (question about OverlapSphere?) 0 Answers
How come certain Children don't cast an Overlapsphere? 1 Answer