- Home /
Enemy and gun damge script not working.
I have a enemy script and a gun damage script which uses raycasting but my enemy wont take damage and loose health can someone please check whats wrong with this script. Enemy script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyScript : MonoBehaviour
{
public int EnemyHealth = 10;
void deductPoints(int DamageAmount)
{
EnemyHealth -= DamageAmount;
}
void Update()
{
if (EnemyHealth <= 0)
{
Destroy(gameObject);
}
}
}
Gun damage script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HandGunDamage : MonoBehaviour
{
public int DamageAmount = 5;
public float TargetDistance;
public float AllowedRange = 15.0f;
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
RaycastHit Shot;
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out Shot))
{
TargetDistance = Shot.distance;
if (TargetDistance < AllowedRange)
{
Shot.transform.SendMessage("DeductPoints", DamageAmount);
}
}
}
}
} Please help me i have been searching a solution of this for some time now but i cant find a fix for this and i know there was a similiar post like this but i didnt help me. Thanks in advance.
,
I'm still a beginner myself when it comes to rays, so ill add a comment rather than an answer. I can't see anywhere you're even trying to communicate with the enemy?
Found this on a post from 2013, so not sure if it's relevent but it might help you.
shot.collider.gameObject.GetComponent<EnemyScript >().DeductPoints(1);
$$anonymous$$ight also be useful to use something like
if (shot.transform.tag=="Enemy"
first. Best of luck
actually communication is happening because in the hierachy i attached the health script to the enemy and i attached the gun damage script to a empty object which i attached to the player
Answer by Hellium · Jul 28, 2019 at 07:55 AM
Divinitize1 is right, you have a big flaw in how you call your function on the enemy. SendMessage
should be called only when prototyping.
Instead, you should simply check whether the hit gameObject (or one of its children) has the desired component and having a tag on the enemy will reduce unnecessary calls to GetComponent
.
So tag your enemies gameObjects (and all their children) Enemy
and change your weapon code to:
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
RaycastHit shot;
bool hittingEnemy = Physics.Raycast(transform.position, transform.forward, out shot) &&
shot.distance < AllowedRange &&
shot.transform.CompareTag("Enemy") ;
if (hittingEnemy)
{
EnemyScript enemy = GetEnemyScript( shot.transform );
if (enemy != null)
{
enemy.DeductPoints(DamageAmount);
}
else
{
Debug.LogError("An enemy does not have the EnemyScript component attached");
}
}
}
}
private EnemyScript GetEnemyScript( Transform hitObject )
{
EnemyScript enemy = hitObject.GetComponentInChildren<EnemyScript>();
while( enemy == null && hitObject.CompareTag("Enemy"))
{
hitObject = hitObject.parent;
enemy = hitObject.GetComponent<EnemyScript>();
}
return enemy;
}
@Hellium Thank you for replying can u please tell me where I should insert this code or whether I should replace And should I attach the script to the enemy in the hierarchy. Please help.
@Hellium I typed out this script but a errors pop up. can u please check on this. EnemyScript.DeductPoints(int)' is inaccesible due to its protection level
As I have indicated, this code must replace your current Update
method of your HandGunDamage
script.
About your error, add public
before void deductPoints(int DamageAmount)
un your enemy script.
@Hellium when i type public void deductPoints(int DamageAmount); another error pops up the modifier public is not valid for this term. btw thanks for helping me out, I really want to make this game.
public int EnemyHealth = 10;
public void deductPoints(int DamageAmount)
{
EnemyHealth -= DamageAmount;
}
Answer by leminh_2014 · Jul 28, 2019 at 11:54 AM
let you try with event system? i think that might good choice in this case.