- Home /
NullReferenceException: Object reference not set to an instance of an object Raycast...?
My melee attack raycast script I got from a tutorial video doesn't seem to be working? Any help I can get is appreciated thank you :) !
Error:
NullReferenceException: Object reference not set to an instance of an object MeleeAttack.attack () (at Assets/Custom Assets/Scripts/Player/MeleeAttack.js:36) MeleeAttack.Update () (at Assets/Custom Assets/Scripts/Player/MeleeAttack.js:24)
MeleeAttack.js
var countdown : int = 2; var playerAttackRange : int = 5; var particle : GameObject; var player : GameObject;
function Start () {
}
function Update () { var particleClone = particle; if(countdown > 0){ countdown -= Time.deltaTime; } if(countdown <= 0){ countdown = 2; }
if(Input.GetButton("Fire1")){
particleClone = Instantiate(particle, transform.position, transform.rotation);
Destroy(particleClone, 1);
attack();
}
}
function attack(){ if(AbilitySystem.fireElement.equippedFire == true) {
var hit : RaycastHit;
var fwd = transform.TransformDirection (Vector3.forward);
if(Physics.Raycast (transform.position, fwd, playerAttackRange)){
if(hit.collider.gameObject.tag == "Enemy") {
Debug.Log("Melee has struck" + hit.collider.gameObject + "!!!!!!!!!!!");
hit.collider.gameObject.GetCompponent(EnemyHealth).enemyCurrentHealth -= AbilitySystem.fireElement.swordDamage;
}
}
}
}
Answer by simonmc · Apr 05, 2012 at 10:39 PM
you need to give your RaycastHit object as a paramater to Physics.Raycast.
E.G
if(Physics.Raycast (transform.position, fwd, playerAttackRange)){
Should be:
if(Physics.Raycast (transform.position, fwd, hit, playerAttackRange)){