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 /
This question was closed Oct 07, 2013 at 05:40 AM by clunk47 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by alexlam127 · Aug 14, 2013 at 03:33 AM · gamebasic

each enemy have different damage amount, how to calculate the hp remain???

so lets say there are different enemy

each enemy attach a script with damage variable

than player trigger by the collide of enemy to access the damage component?Is is workable? is there better method??

 collider enermy1;
 
 damage = enermy1.GetComponent<damagescript>().damagevariable;

 hp = hp-damage;
Comment
Add comment · Show 3
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 alexlam127 · Aug 14, 2013 at 03:31 AM 0
Share

im worrying that since my game target mobile platform if there is many enemy around, "GetComponent" would cause performance issue...

avatar image YoungDeveloper · Aug 14, 2013 at 04:00 AM 0
Share

Everything's fine, don't worry.

avatar image vexe · Sep 26, 2013 at 05:38 PM 1
Share

HELLO dear OP? Please tick an answer to keep the board tidy and clean. If an answer wasn't helpful to you, proper feedback would be appreciated. Thanks.

1 Reply

  • Sort: 
avatar image
2
Best Answer

Answer by vexe · Aug 14, 2013 at 07:19 AM

You can approach it the other way around. You say there are dif enemies, each have dif damages amounts. But there is one player, right? ;) - Let all your types of enemies have a reference to your player. Set the ref at your enemy Start:

 Player player;
 void Start() // or function Start() on JS
 {
    player = GameObject.FindWithTag("Player");
    // ... rest of your stuff
 }

That way, you don't have to take the damage variable from your enemy, teach time they hit you. Instead, you do the opposite:

 if (collider is an enemy)
    player.InflictDamage(damage);

And let the player decide how he should take damage:

 void InflictDamage(int damage)
   if (hp > 0)
     hp -= damage;

You can also have a game controller, that controls your whole game. It could act as an interface of communication between all your objects, enemies, players, etc. So when your enemy hits you, you can notify the gameController that your player has taken damage, and it will take the necessary measure

 gameController.Notify_PlayerHit(enemyDamageAmount);
Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

17 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

Related Questions

Multiple Cars not working 1 Answer

MiniMap Icons 0 Answers

How do i make an animation play on key press? 3 Answers

Getting Error Object reference not set to an instance of an object 1 Answer

Trap Door Question 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