- Home /
[SOLVED] Enemy Script : Mob doesn't take damage
=-=-=-=-=-=-=-=-=-=-=-=-=
I solved my problem finally.
=-=-=-=-=-=-=-=-=-=-=-=-=
Hi. I'm currently working on a little FPS game. It is the first game that I do. I got a problem. My mob doesn't take damage.
Can you help me ?
ShootScript.js is on the camera of the player. EnemyScript.js is on the mob.
Here is my code :
File : ShootScript.js
var fireRate = 0.30;
var impact:GameObject;
var canFire = true;
var sound:AudioClip[];
var currentAmmo = 10;
var maxAmmo = 10;
var reloadTime = 2;
var damage:float=10;
Screen.showCursor = false;
function Update () {
if (Input.GetButton("Fire1")) {
if (canFire==true) {
canFire = false;
fire();
}
}
}
function fire() {
if (currentAmmo >0) {
fireOneShot();
currentAmmo = currentAmmo - 1;
if (currentAmmo==0) {
audio.PlayOneShot(sound[1]);
yield WaitForSeconds(reloadTime);
currentAmmo = maxAmmo;
audio.PlayOneShot(sound[2]);
canFire = true;
}
}
}
function fireOneShot() {
audio.PlayOneShot(sound[0]);
var direction = transform.TransformDirection(Vector3.forward);
var hit:RaycastHit;
if (Physics.Raycast(transform.position, direction, hit, 100)) {
tempRot=Quaternion.FromToRotation(Vector3.up, hit.normal);
Instantiate(impact, hit.point, tempRot);
if (hit.rigidbody) {
hit.rigidbody.AddForce(1000*direction);
}
hit.collider.SendMessageUpwards("makeDamage", damage, SendMessageOptions.DontRequireReceiver);
}
yield WaitForSeconds(fireRate);
canFire = true;
}
File : EnemyScript.js
var hitPoints = 10;
function makeDamage(damage:float) {
hitPoints -= damage;
if (hitPoints <= 0) {
Destroy(gameObject);
}
}
Sorry for my English, it is not my first language.
Edit : I change "DontRequireReveicer" for "DontRequireReceiver". Second problem now.
Answer by Laurent11 · Dec 23, 2014 at 07:20 PM
I just notice that there was an error in the line 56. Now, my mob doesn't take damage.
Answer by Thom Denick · Dec 23, 2014 at 07:19 PM
I accidentally approved this question, so I'll answer it. NullReferenceException means you have an object that hasn't been assigned (and has a null value), and you are trying to do something with it.
There are literally thousands of questions about this on Unity Answers. Search NullReferenceException if you still need help. In the future, please search UA before submitting a question.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
get closest enemy 1 Answer
GuiTexture Width Change 1 Answer
Enemy Spawn script not continuing level 2 Answers
Help needed with a pickup script 2 Answers