Hey all,
I posted about this previously but have not been able to get something in my game to work. I am making an underwater FPS style game where the player can shoot harpoon objects. I want the enemies to take damage when the harpoon collides with them. Unfortunately, I cannot seem to get the harpoondamage script to talk to the damagetaker script that I am using. I am very very new to scripting (having primarily worked as an artist) but on this project I’m pretty much the only one crazy enough to take on the task. Here are both of them:
HarpoonDamage:
var harpoonDamage = 10.0;
var hitEffect : GameObject;
private var hit : Collision;
function OnCollisionEnter() {
//Send a damage message to the hit object
if (hit.collider.tag == "Enemy" || hit.collider.tag == "Projectile")
{
/*Instantiate(hitEffect, hit.point, Quaternion.identity);*/
hit.collider.SendMessage("ApplyDamage", harpoonDamage, SendMessageOptions.DontRequireReceiver);
}
}
DamageReceiver:
var hitPoints = 100.0;
var detonationDelay = 0.0;
var explosion : Transform;
var deadReplacement : Rigidbody;
function ApplyDamage(damage : float){
//We already have less than 0 hitpoints, maybe we got killed already?
if(hitPoints <= 0.0)
return;
hitPoints -= damage;
if(hitPoints <= 0.0){
//Start emitting particles
var emitter : ParticleEmitter = GetComponentInChildren(ParticleEmitter);
if(emitter)
emitter.emit = true;
Invoke("DelayedDetonate", detonationDelay);
}
}
function DelayedDetonate(){
BroadcastMessage("Detonate");
}
function Detonate(){
//Destroy ourselves
Destroy(gameObject);
//Create the explosion
if(explosion)
Instantiate(explosion, transform.position, transform.rotation);
//If we have a dead barrel then replace ourselves with it!
if(deadReplacement){
var dead : Rigidbody = Instantiate(deadReplacement, transform.position, transform.rotation);
//For better effect we assign the same velocity to the exploded barrel
dead.rigidbody.velocity = rigidbody.velocity;
dead.angularVelocity = rigidbody.angularVelocity;
}
//If there is a particle emitter stop emitting and detach it so it doesnt get destroyed right away
var emitter : ParticleEmitter = GetComponentInChildren(ParticleEmitter);
if(emitter){
emitter.emit = false;
emitter.transform.parent = null;
}
}
//We require the barrel to be a rigidbody, so that it can do nice physics
@script RequireComponent(Rigidbody)
I always seem to get this error message: NullReferenceException: Object reference not set to an instance of an object
HarpoonDamage.OnCollisionEnter () (at Assets/Scripts/HarpoonDamage.js:9)
Whenever my harpoons hit anything. Can someone please help?
BTW…I have done both the FPS and platformer tutorials. Some of the scripts are derived from them but I still cannot get them to work.