- Home /
enemy not dies when player attacks and health value not Decreasing.
Enemies not dying and their health not decreasing, when player attacks them .There is scripts below for player attack(Gun.cs) and enemy health(Adg.cs) .pls solve if my code is wrong?!
Gun.cs
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Gun : MonoBehaviour {
protected PlayerController playerController;
public FixedButton Button;
[SerializeField]
[Range(0.1f, 1.5f)]
private float fireRate = 0.1f;
private int damage = 1;
[SerializeField]
private ParticleSystem muzzleParticle;
[SerializeField]
private AudioSource gunFireSource;
private float timer;
// Update is called once per frame
void start()
{
playerController = GetComponent<PlayerController>();
playerController.SetArsenal("Rifle");
}
void Update()
{
timer += Time.deltaTime;
if(timer >=fireRate)
{
if (Button.Pressed)
{
timer = 0f;
FireGun();
}
}
}
private void FireGun()
{
muzzleParticle.Play();
gunFireSource.Play();
Ray ray = Camera.main.ViewportPointToRay(Vector3.one * 0.5f);
Debug.DrawRay(ray.origin, ray.direction * 100, Color.red, 2f);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo, 100))
{
var health = hitInfo.collider.GetComponent<adg>();
if (health != null)
health.TakeDamage(damage);
}
}
}
Adg.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI;
public class adg : MonoBehaviour { public int level; private AudioSource acf; private OneThirdPersonController Chh; private Gun gun; public float restartDelay = 1f;
private NavMeshAgent nav;
private bnm remov;
private SphereCollider sph;
private mkl fbh;
private Animator animator;
[SerializeField]
public float maxHealth = 10;
public float currentHealth;
public void Start()
{
acf = GetComponent<AudioSource>();
Chh = GetComponent<OneThirdPersonController>();
nav = GetComponent<NavMeshAgent>();
sph = GetComponent<SphereCollider>();
remov = GetComponent<bnm>();
fbh = GetComponent<mkl>();
animator = GetComponent<Animator>();
currentHealth = maxHealth;
}
public void TakeDamage(int damageAmount)
{
int i = 0;
currentHealth -= damageAmount;
if (currentHealth <= i)
{
Die();
}
}
public void Die()
{
animator.SetTrigger("Die");
FindObjectOfType<GameManager>().EndGame();
nav.enabled = false;
remov.enabled = false;
sph.enabled = false;
fbh.enabled = false;
}
}
Answer by antonsem · Sep 02, 2021 at 09:47 AM
We don't know how you scene and objects are set up. So instead of solving your issues I will tell you how you can debug it and find the answer yourself.
Here is what suppose to happen:
timer >= fireRate
Button is pressed
FireGun() method is invoked
Ray is fired
Ray is hitting the object
Ray gets the component
If the component is found TakeDamage method is invoked
If current health <= 0 Die is invoked
Make sure that this is what happens. And if not then you can investigate further. For example, if ray doens't hit the object make sure that the ray is shooting towards the obejct, if the object is close enough etc.
Thanks for reply!! I debug and found till first Five points it works, When ray passes through object or enemy the component adg not found.What the major problem is it works sometimes above and not other time.pls solve!!
I can't solve it remotely. As I mentioned in my answer you will have to debug it yourself. You said that the component is not found on the object. The next logical question is "Why?" Is it the same object you are hitting? Is the component still there? Why is it not there? Have you destroyed the component somewhere? Did you forget to add it back? What happens after five points? Ask yourself these questions and try to find answers for them.
PS: "pls solve!!" is not a very nice way to ask for help on the internet. I assume you are not a native English speaker and you are not trying to order anyone around. But when you say it, it comes across that way. Try something like "Could you help me please?" "What am I doing wrong?" "Any help is appreciated." and you will get more help much faster :)
The enemy is dying sometimes because the code works sometimes and not at othertimes(component adg founds sometimes only).So,I am helpless
Your answer
Follow this Question
Related Questions
Enemy won't damage player C# 1 Answer
Enemy wont damage player 0 Answers
Enemy wont damage player 2 Answers
How to make multiple Enemy Health Bars to work? 2 Answers
how to destroy enemy 1 Answer