So…My gameobject won’t get destroyed. I can play the game just fine, but i got this error:
NullReferenceException: Object reference not set to an instance of an object
AxeAttack.Attack () (at Assets/Scripts/AxeAttack.js:47)
AxeAttack.Update () (at Assets/Scripts/AxeAttack.js:26)
Here is my health script:
#pragma strict
var axe : GameObject;
private var axeScript : AxeAttack;
var health : float = 100.0;
function Start ()
{
axeScript = axe.GetComponent(AxeAttack);
}
function update ()
{
if(health <= 0.0)
{
Dead();
}
}
function ApplyDamage (damage : float)
{
health -= axeScript.damage;
}
function Dead ()
{
Destroy(gameObject);
}
And here is my axe script:
#pragma strict
private var isAttacking : boolean = true;
private var attackCoolDown = 0.0;
var damage = 50;
var range = 5;
var layerMask : LayerMask;
private var mainCamera : GameObject;
function Start () {
isAttacking = false;
mainCamera = GameObject.FindWithTag("MainCamera");
}
function Update () {
if(isAttacking)
{
attackCoolDown += Time.deltaTime;
if(attackCoolDown >= 2)
{
isAttacking = false;
attackCoolDown = 0.0;
}
}
if(Input.GetMouseButtonDown(0) && isAttacking == false)
{
Attack();
}
if(isAttacking == false)
{
this.gameObject.animation["idle_cube"].wrapMode = WrapMode.Loop;
this.gameObject.animation.CrossFade("idle_cube");
}
}
function Attack ()
{
this.gameObject.animation.CrossFade("attack_cube");
isAttacking = true;
attackCoolDown = 1;
var direction = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;
Debug.DrawLine(mainCamera.transform.position,direction*range,Color.red);
if (Physics.Raycast (mainCamera.transform.position, direction, hit, range, layerMask));
{
if(hit.transform.tag == "Enemy")
{
hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
}
}
}