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 darkironphoenix · Jan 27, 2013 at 01:06 AM · arrayvalue

Adding array values together

So, i'm being a fish out of water again...yey.

I built a simple array :

 var itemList = new Item[40];

 class Item {
     var itemName : String;
     var sellPrice : int;
     var weight : float;
     var quantityInInventory : int;
 }

And then set about populating the list. Now for the dillemma. How do i add all the weights of each item together then display the total?

I am using javascript.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Mikilo · Jan 29, 2013 at 12:40 AM

Hi,

The code in C# looks like that:

 float    total = 0;
 for (int i = 0; i < items.Length; i++)
 {
     total += items[i].weight;
 }
 return total;

In JS:

 class Item
 {
     var weight : float;
 }

 var items : Item[];
 function GetTotal()
 {
     var total = 0.0f;
     for (var i = 0; i < items.Length; i++)
     {
         total += items[i].weight;
     }
     return total;
 }
Comment
Add comment · Show 3 · 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 darkironphoenix · Jan 29, 2013 at 01:40 AM 0
Share

As i comented above i'm using javascript and i'm not that confident with code. Using my rudementary knowledge, i tried to convert your code to javascript but it kept throwing up the following error:

Assets/Scripts/Test/ItemDictionary.js(20,12): BCE0044: expecting ;, found 'i'.

avatar image Mikilo · Jan 29, 2013 at 08:32 AM 0
Share

I added the JS version.

avatar image alexjhones286 · Feb 01, 2019 at 03:04 PM 0
Share

Obrigado'. Brasil aqui'. :)

avatar image
0

Answer by $$anonymous$$ · Jan 27, 2013 at 01:17 AM

You don't have a list or array in this code snippet but I guess you have a list of these items like so:

 var items : List.<Item>();

Then you add items with for example:

 items.Add(someItem);

You calculate the total weight with:

 float TotalWeightOfItems() {
    float totalWeight = 0;
    foreach (item in items) {
       totalWeight += item.weight;
    }
    return totalWeight;
 }
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 darkironphoenix · Jan 28, 2013 at 11:28 PM 0
Share

Sorry, im using

 var itemList = new Item[40];

Where do i put your code snippet, it returns with wanting to put a semi colon after float.

I'm using javascipt.

avatar image
0

Answer by toddisarockstar · Feb 02, 2019 at 04:12 AM

 var itemList = new Item[40];
  class Item {
      var itemName : String;
      var sellPrice : int;
      var weight : float;
      var quantityInInventory : int;
  }
  
  // set some values
 itemList[2].sellPrice=23;
 itemList[5].sellPrice=18;
 itemList[8].sellPrice=3;
 
 var total : int=0;
 for(var it : Item in itemList){
 total+=it.sellPrice;}
 
 print(total);
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

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

12 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

Related Questions

Add values to Int[] (SetTriangles) 1 Answer

Check if all elements of an array contain the same value? 3 Answers

How to get the key from Array value 1 Answer

How to check if vector in array was not changed? 1 Answer

Saving values generated in game to use in prefabs. 2 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