Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Loki18000 · Sep 02, 2015 at 09:29 AM · c#raycastprefab

Destory a single prefab when hit with Raycast

Hello, I am having an issue with Destroying a single prefab by lowering a single prefabs int to 0 without doing so to all other prefabs.

So the player "shoots" at a enemy that has a certain amount of "health", once it hits 0 the gameObject is destroyed. But my script takes the amount of "Damage" from all prefabs and not just the one that is being hit.

Here is the shooting script

     void FixedUpdate() {
         if (shooting) {
             shooting = false;
             flash.GetComponent<ParticleSystem>().Play();
 
             RaycastHit hit;
             Debug.DrawRay(transform.position, -transform.right, Color.green, 50f);
             if(Physics.Raycast(transform.position, -transform.right, out hit, 50f)) {
                 if(hit.transform.tag == "Enemy") {
                     Instantiate(impact, hit.point, transform.rotation);
 
                     enemyAttack.enemyHp -= 5;
                     if(enemyAttack.enemyHp < 0) {
                         Destroy(hit.collider.gameObject);
                     }
                 }
             }
         }
     }


The enemies Health is being controlled by a script that is on the prefabs. It is just

 public static int enemyHp = 200;

This at some degree does work, if the player shoots at an enemy enough then it will be destroyed but all other prefabs enemyHp is at 0. So what is taken from on prefab is taken from the others. and then each prefab is destroyed with a single hit after that.

Thank you for any help guys

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

Answer by pako · Sep 02, 2015 at 01:07 PM

You have made the enemyHp a static field. This means that enemyHp is not part of each instance of the enemy prefab, but it applies to all instances of the enemy prefab. So, remove the static keyword, and then each enemy will have its own enemyHp:

 public int enemyHp = 200;

This way, only the enemy instance that is being hit by the player will have its health reduced.

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 Loki18000 · Sep 03, 2015 at 11:21 AM 0
Share

I will give that ago, I am not even sure why it is static, not sure why I made it Static. Thanks for pointing it out!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to effect and apply a common material to children individually... 2 Answers

Multiple Cars not working 1 Answer

Grid Placement In Game 2 Answers

How to spawn prefab only in allowed area? 0 Answers

Spawned tree not falling 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