- Home /
Other: Error was identified and did not relate to the questions asked. I do not want to mark my own comment as an answer.
Object Not Dying
Hi Everybody,
I am sorry if this is a stupid question, I am new to Unity and trying to learn from some tutorials, etc. I have looked around for help before asking, and was not able to find anything.
I am trying to make a fps and am just building things up slowly. I am trying to make an object disappear when it gets show and its health reaches 0 or less.
The code if have for the gun is:
var DamageAmount : int = 5;
var TargetDistance : float;
var AllowedRange : float = 15;
function Update () {
if(Input.GetButtonDown("Fire1")) {
var Shot : RaycastHit;
if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), Shot)) {
TargetDistance = Shot.distance;
if (TargetDistance < AllowedRange) {
Shot.transform.SendMessage("DeductHealth", DamageAmount, SendMessageOptions.DontRequireReceiver);
}
}
}
}
and The code of I have the object that should disappear is
var EnemyHealth : int = 10;
function DeductHealth (DamageAmount : int) {
EnemyHealth -= DamageAmount;
}
function Update () {
if (EnemyHealth <= 0) {
Destroy(gameObject);
}
}
My understanding is the first gun shot that hits the target and is within the range of 15 should take thier health down to 5, the second one should take it to 0 which should trigger the 'destroy(gameObject)'.
Nothing seems to happen at all when I shoot. If I have the GunMechanics selected (located under FirstPersonCharacter) on the Inspector, I can see the 'Target Distance' value change so the shot is being acknowledged at least in part.
If you need any more informaiton to answer, please let me know.
Thanks in Advance.
Try to edit this
function DeductHealth
to
public function DeductHealth
and
Shot.transform.Send$$anonymous$$essage("DeductHealth", DamageAmount, Send$$anonymous$$essageOptions.DontRequireReceiver);
to
Shot.gameObject.GetComponent("Class name of enemy health").DeductHealth(DamageAmount);
Answer by WaywardKnight · Nov 04, 2016 at 11:36 PM
Thanks for the quick response @JScotty. Unfortunatly, it didnt help.
but, now here is the stupid thing...I just figured put the issue....The object the gun script was attached too...it was rotated on the Y axes by 180 degrees...I don't know how that happend. But yeah, fixing that made it work perfectly...
Like I said, I greatly appreciate the quick response and I am sure if you could see the whole project you would have figured that out in no time.