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
1
Question by RyanRogers · Jul 14, 2020 at 03:24 PM · damagedictionarycollections

Keeping track of damage damage taken by which enemies

If I have an entity that can take damage from multiple sources, and I want to keep track of how much damage each source has given the entity, what is the best way to go about keeping track?

For example, I have an NPC, and two enemies. Enemy1 deals 5 damage to the NPC, and Enemy2 deals 6 damage. After that, Enemy1 deals another 5 damage. So I want to be able to check a field on the NPC and know that Enemy1 has dealt 10 damage in total.

The only way I can think of currently is to have a dictionary on the NPC with the Key being the enemy and the value being the total damage, and then update that value when more damage is taken. This seems a but clunky however, so I wanted to see if maybe I'm missing out on a more elegant solution.

Any ideas would be appreciated. Thanks!

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 AlvinIsCrack123 · Jul 14, 2020 at 04:23 PM

(Note : Before you start reading, sorry for my bad english. I ain't a native english speaker)


I recommend you checking this post which tries to find the better solution for storing values along a time (values can update on time progress).

"If the attributes that you are tracking are known ahead of time, then a class is the way to go."

So you could create a public static class (not derived from any other class) and put in the assets folder (to give Unity access to the C# script). Inside that script you put public static variables which corresponds to Enemy0_totalDamage // Enemy1_totalDamage... etc.

 public static class GameVariables
 {
     public static class EnemyDamage
      {
       //And there you put your code/variables that stores the total damage of each enemy on player. 
       //(You will need to acess this script from other scripts to change values, obviously).
 
       public static int TotalEnemy0_damage = 0;
       public static int TotalEnemy1_damage = 0;
       //And so on...
      }
 }

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 RyanRogers · Jul 14, 2020 at 07:09 PM 0
Share

Thanks for the link and explanation!

Based on what the link is saying, since I don't entirely know how many things I need to keep track of, it looks like I should go with the dictionary ins$$anonymous$$d of the static variables.

avatar image fafase RyanRogers · Jul 14, 2020 at 07:16 PM 2
Share

Yes, unfortunately, using static is not the solution, it would mean that all NPC's would share the same info and that is not what you want.

Use the dictionary you had in $$anonymous$$d, it works just fine and is the most optimized way.

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

132 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

Related Questions

Dictionary Returning Null Value 1 Answer

OrderedDictionary in System.Collections.Generic for mobile 0 Answers

OrderedDictionary on Mobile 0 Answers

How to organize challenge system? 0 Answers

Saving dictionary from editor mode in play mode 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