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 Flint Silver · May 02, 2014 at 04:47 PM · c#listclassspace

How to sum a property of a class in a List

I'm using lists to be able to use more than one engine in my SpaceShip game. So, there is the engine Class that has variable ForceGenerated and FuelCapacity.

How I can be able to use List<>.Sum(all variable ForceGenerated) I don't know the syntax to use this Sum (I saw docs on Microsoft, but I still don't understand)

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by xortrox · May 02, 2014 at 04:57 PM

 float totalForceGenerated = 0.0f;
 float totalFuelCapacity = 0.0f;
 foreach(NameOfEngineClass e in nameOfList)
 {
 
     totalForceGenerated += e.ForceGenerated;
     totalFuelCapacity += e.FuelCapacity;
 
 }
Comment
Add comment · Show 4 · 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 xortrox · May 02, 2014 at 04:58 PM 0
Share

alternative using normal for:

float totalForceGenerated = 0.0f; float totalFuelCapacity = 0.0f; for(int i = 0; i < nameOfList.Count; i++) {

 totalForceGenerated += nameOfList[i].ForceGenerated;
 totalFuelCapacity += nameOfList[i].FuelCapacity;
 

}

avatar image Flint Silver · May 02, 2014 at 05:17 PM 0
Share

Yes true... I've thought to do this but I'm a bit stupid (Lol). I didn't thought how to calculate it every frame that was a easy problem.

But none know the syntax of List.Sum(variables of the ListClass)?

avatar image jeebxoxo · May 02, 2014 at 05:46 PM 0
Share

I wouldn't use List.Sum. Not only is this a pretty simple thing to do with a loop but some platforms (like iOS) don't play nicely with those functions on a generic.

avatar image Flint Silver · May 03, 2014 at 02:35 PM 0
Share

Yeah, but I've to check 2 different list to check with this method (2 loops + another that I use to "update" the current fuel) and it seems to me a heavy script, so I thought that maybe List.Sum was cheaper.

Is it correct?

P.s. If only iOS doesn't support it, isn't a problem...

avatar image
1

Answer by jeebxoxo · May 02, 2014 at 05:01 PM

Just loop through your list and add them all in a variable.

 public float totalForce;
 
 // you probably already have this list elsewhere I'm just showing it so you see where I get 'engines'
 public List<Engine> engines = new List<Engine>();
 
 
 void AllForce () {
   totalForce = 0.0f;
  
   foreach(Engine e in engines) {
     totalForce += e.ForceGenerated;
   }
 
 }
 
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 SeeMeNot · Apr 15, 2017 at 11:14 PM 0
Share

im trying to use something similar to this and watch it in the update. it keeps adding to it

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

23 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

Related Questions

Removing a class object from a list(.remove not working) C# 1 Answer

C# List and GUI 3 Answers

Can't call derived class function from list of base class. Ideas? 1 Answer

Need my function to work with different lists of different values (classes) 1 Answer

Best structure for a perk system? 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