Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by kiran1 · Sep 02, 2021 at 07:52 AM · unity 5enemy healthenemydamage

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;





 }

   

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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:

  1. timer >= fireRate

  2. Button is pressed

  3. FireGun() method is invoked

  4. Ray is fired

  5. Ray is hitting the object

  6. Ray gets the component

  7. If the component is found TakeDamage method is invoked

  8. 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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kiran1 · Sep 02, 2021 at 10:15 AM 0
Share

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!!

avatar image antonsem kiran1 · Sep 02, 2021 at 10:26 AM 0
Share

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 :)

avatar image kiran1 antonsem · Sep 02, 2021 at 10:59 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

221 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges