Question by
Slashscreen · Jul 09, 2016 at 01:56 AM ·
javascripterrornullreferenceexceptionmessages
I really need help with this message
I am sending a message to an object, asking it to print some numbers for testing, but when I activate it, it returns a NullReferenceException. What did I do wrong?
Sending End: Declare:
var bomb : GameObject;
Send:
bomb = gameObject.Find("BombBrick");
bomb.SendMessage("explode", 10);
Receiving End:
function explode(data){
//if(data === gameObject){
var radius = 10;
var explosionPos: Vector3 = gameObject.transform.position;
var colliders: Collider[] = Physics.OverlapSphere(explosionPos, radius);
for (var otherHit: Collider in colliders) {
var rb: Rigidbody = otherHit.GetComponent.<Rigidbody>();
if (rb != null)
rb.AddExplosionForce(300, explosionPos, radius, 3.0F);
if (otherHit.GetComponent.<Collider>().tag=="Regular"){
Dotscore = Dotscore + 1;
Destroy(otherHit.GetComponent.<Collider>().gameObject, 1);
};
if (otherHit.GetComponent.<Collider>().tag=="Freeze"){
Destroy(otherHit.GetComponent.<Collider>().GetComponent.<Rigidbody>());
};
if (otherHit.GetComponent.<Collider>().tag=="Boom"){
};
};
Destroy(gameObject);
Comment
I figured it out! Here's my current code: Send:
var bomb = gameObject.Find(hit.collider.gameObject.name);
bomb.Send$$anonymous$$essage("explode");
Receive:
function explode(){
What I had done wrong was: -Did not gameObject.Find right object name -Did not need to do initial declare as public variable, only needed private -Since I need to send message to clone, I needed the gameObject of the Clone. -Did not need argument