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 Hefaz · Dec 14, 2020 at 04:01 PM · prefabenemydamagehealth

Enemy Damage

It has been 7 days, that I am trying to solve this problem, But I can not figure it out. Please help me.

I have an enemyHealth script something like this:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class EnemyHealth : MonoBehaviour
 {
     public float maxHealth = 100f;
     public float currentHealth;
 
     public bool isDead = false;
     Animator enemyAnim;
     private void OnEnable()
     {
         currentHealth = maxHealth;
         enemyAnim = GameObject.FindGameObjectWithTag("Enemy").GetComponentInChildren<Animator>();
     }
 
     public void TakeDamage(float damageAmount)
     {
         currentHealth -= damageAmount;
         if(currentHealth <= 0)
         {
             Die();
         }
     }
 
     public void Die()
     {
         enemyAnim.SetTrigger("isDeadTrigger");
         isDead = true;
     }
 }
 

and then I have a playerGun script, where i shoot the enemies. it is something like this:

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class PlayerGun : MonoBehaviour
 {
     int id;
     [SerializeField]
     int currAmmo;
     float reloadSpeed = 3;
     public AudioSource source;
     public AudioClip gunSound;
     public AudioClip reloadSound;
     float impactForce = 30f;
     public float fireRate = 0.1f;
     public GameObject enemyCarrate;
 
     public int coins;
     public Text coinsText;
 
     GameObject gameController;
     WeaponDatabase database;
 
     Animator animator;
     Animator enemyAnim;
 
     EnemyHealth enemyStat;
 
     public ParticleSystem muzzleFlash;
     public GameObject impactEffect;
 
     bool reloading = false;
 
     private float timer;
 
     private void Awake()
     {
         source = GetComponent<AudioSource>();
     }
 
     private void Start()
     {
         gameController = GameObject.FindGameObjectWithTag("GameController");
         database = gameController.GetComponent<WeaponDatabase>();
         id = GameObject.FindGameObjectWithTag("Weapon").GetComponent<ItemID>().itemID;
         currAmmo = database.weapons[id].maxAmmo;
         animator = GameObject.FindGameObjectWithTag("Player").GetComponentInChildren<Animator>();
         enemyAnim = GameObject.FindGameObjectWithTag("Enemy").GetComponentInChildren<Animator>();
         coins = 0;
     }
 
     private void Update()
     {
         if (Input.GetKey(KeyCode.R) && !reloading)
         {
             if (currAmmo == database.weapons[id].maxAmmo)
             {
                 return;
             }
             else
             {
                 
                 StartCoroutine(Reload());
                 //stop firing while reloading
                 return;
             }
         }
         if(currAmmo<=0)
         {
             StartCoroutine(Reload());
             //stop firing while reloading
             return;
         }
         timer += Time.deltaTime;
         if (timer >= fireRate)
         {
             if (Input.GetButton("Fire1"))
             {
                 timer = 0;
                 Fire();
             }
         }
         /*
         if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
         {
             Fire(); 
         }
         */
     }
 
     IEnumerator Reload()
     {
         reloading = true;
         source.PlayOneShot(reloadSound, 0.3f);
         animator.SetBool("reloading", true);
         yield return new WaitForSeconds(reloadSpeed);
         currAmmo = database.weapons[id].maxAmmo;
         reloading = false;
         animator.SetBool("reloading", false);
     }
 
     void Fire()
     {
         muzzleFlash.Play();
         source.PlayOneShot(gunSound, 0.3f);
         currAmmo--;
         animator.SetInteger("condition", 6);
        
         RaycastHit hit;
         
         if (Physics.Raycast(transform.position, Camera.main.transform.forward, out hit, database.weapons[id].range))
         {
             enemyStat = hit.transform.GetComponent<EnemyHealth>();
             if (hit.transform.tag == "Enemy")
             {
                 if (!enemyStat.isDead)
                 {
                     enemyStat.TakeDamage(database.weapons[id].damage);
 
                     if (enemyStat.currentHealth <= 0)
                     {
 
                         enemyStat.isDead = true;
                         //Instantiate(enemyCarrate, enemyStat.transform.position, enemyStat.transform.rotation);
                         coins = coins + 10;
                         coinsText.text = coins.ToString();
                         // enemyAnim.SetTrigger("isDeadTrigger");
                         enemyStat.Die();
                     }
                 }
             }
 
             if (hit.rigidbody != null)
             {
                 hit.rigidbody.AddForce(-hit.normal * impactForce);
             }
             GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
             Destroy(impactGO, 1f);
             
         }
     }
 
 
 }
 

the enemyHealth script is attached to the enemy prefab and the playerGun script is attached to the palyer prefab.

When I have only one enemy in the scene. it works fine and it kills the enemy and i get some coins. but I bring more types of the enemy prefabs, still works only for one. at the same time the health of all enemies reduces when i shoot at one.

Please help me to find the error. I really don't know what to do.

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
1
Best Answer

Answer by xxmariofer · Dec 14, 2020 at 04:21 PM

You cant find the script like this

 enemyAnim = GameObject.FindGameObjectWithTag("Enemy").GetComponentInChildren<Animator>();

if you have multiple enemies you will not be able to know which animator find, you should probably rewrite to this

 enemyAnim = gameObject.GetComponentInChildren<Animator>();

same goes to the Player Script although is already commented you would need to find the enemy that got hit like you did in this line

 enemyStat = hit.transform.GetComponent<EnemyHealth>();
Comment
Add comment · Show 1 · 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 Hefaz · Dec 14, 2020 at 05:12 PM 0
Share

Yes, adding the gameObject worked for me. Thank you very much.

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

184 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

Related Questions

How to make my Player take damage from prefab? 1 Answer

How to make your player health subtract when enemies come into contact with you 3 Answers

Destroy 1 prefab object and changing the color/image 1 Answer

MissingMethodException: UnityEngine.Collision.GetComponent 1 Answer

Prefab shooting damage/health? 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