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 11, 2020 at 01:23 PM · prefabsenemy health

Why does killing one enemy kill them all?

I saw a lot of similar questions but non of them helped me, and they were old too. So I want to ask that in the script below when kill an enemy, automatically the currentHelath of all enemies go down. Can someone tell me what is wrong with the script?

Note: currentHealth is a non-static variable defined in characterStats class. and I call the fire method in PlayerGun script.

 void Fire()
     {
         muzzleFlash.Play();
         gunSound.Play();
         currAmmo--;
         animator.SetInteger("condition", 6);
         RaycastHit hit;
         if (Physics.Raycast(transform.position, Camera.main.transform.forward, out hit, database.weapons[id].range))
         {
             nextTimeToFire = Time.time + 1f/database.weapons[id].fireRate;
             if (hit.transform.tag == "Enemy")
             {
                 CharacterStats enemyStat = hit.transform.GetComponent<CharacterStats>();
                 if (!enemyStat.isDead)
                 {
                     enemyStat.TakeDamage(database.weapons[id].damage);
 
                     if (enemyStat.currentHelath <= 0)
                     {
                         enemyStat.isDead = true;
                         coins = coins + 10;
                         coinsText.text = coins.ToString();
                         enemyAnim.SetTrigger("isDeadTrigger");
                     }
                 }
             }
 
             if (hit.rigidbody != null)
             {
                 hit.rigidbody.AddForce(-hit.normal * impactForce);
             }
             GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
             Destroy(impactGO, 1f);
             
         }
     }
Comment
Add comment · Show 1
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 Hellium · Dec 11, 2020 at 05:30 PM 0
Share
  1. How do you get enemyAnim?

  2. Why isn't it the CharacterStats which handles the dead of the character and its animation?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by CmdrZin · Dec 11, 2020 at 04:57 PM

Try

  if( hit.transform.position == transform.position )
 

or the collider to determine it the ray hit this object. Then check for Enemy tag. Another way to detect a hit is with OnMouseDown() in your target's script.
see https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseDown.html

Comment
Add comment · Show 2 · 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 11, 2020 at 09:03 PM 0
Share

Thank you for the answer, but none of the above works! I don't use any Triggers for this. currently I have two prefabs of enemy in my scene. and when I kill one, the second one dies and falls down. but the one that got the shot, does not fall down, only the health reduces. I think the problem is in CharacterStats enemyStat = hit.transform.GetComponent<CharacterStats>(); since it loads the health script for the enemy. so I think it loads for all and affects for all as well. but I can't figure it out.

avatar image CmdrZin Hefaz · Dec 11, 2020 at 09:37 PM 0
Share

Well, the only other thing would be variables that are static. This would make them common to all prefabs instead of independent for each. Good luck.
Have fun.

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

146 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

Related Questions

Prefab, referencing them, using their methods:bug or intentional? 1 Answer

file sizes of prefabs 0 Answers

Client's list of game object prefabs not shown in the Server. How do I solve this? 0 Answers

i have created a prefabs then how can i manage his multiple clone saptarely and each from given all positions from update function using c# 0 Answers

Can't drag and drop prefabs made by other people on my team (Git/Git LFS) 0 Answers


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