Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

I need to get a total value in an Array, but i can't do it :/

Discussion in 'Scripting' started by bugra271, Mar 4, 2014.

  1. bugra271

    bugra271

    Joined:
    Feb 4, 2014
    Posts:
    17
    I need to get a total value in an Array, but i can't do it :( How can i do this?
     
  2. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    You can use LINQ. It depends on what your array contains. If it's an array of numeric values (like int) you can do this:

    Code (csharp):
    1.  
    2. var total = myArray.Sum(itm => itm);
    3.  
    In this case "itm" is just a variable the represents each array element. But now let's say your array is actually an array of Vector3's and you want to sum up the "x" positions (just using it as an example);

    Code (csharp):
    1.  
    2.  
    3. var total = myArray.Sum(itm => itm.x);
    4.  
    5.  
    That will return the total sum of the "x" property of the objects in your array.
     
  3. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,302
    Show us what you have tried.
     
  4. agimovel

    agimovel

    Joined:
    Feb 20, 2014
    Posts:
    26
    You can set the values of the arrays and then just use a FOR to count the values. I would do somethin close to this (untested code):

    Code (csharp):
    1.  
    2. for(i=0;i<nameOfTheArray.lenght;i++){
    3.     total += nameOfTheArray[i];
    4. }
    5. print(total);
    6.  
     
  5. bugra271

    bugra271

    Joined:
    Feb 4, 2014
    Posts:
    17
    I got dynamic float values which can change at each frame. So i figured this out, and works fine. Thank you all for your help :)

    Code (csharp):
    1. for(var i=0;i<comps.length;i++)
    2. {
    3.         total += comps[i].inputValue;
    4.  
    5.         if( i == 0)
    6.         total = 0;
    7. }
    8.     print(total);
     
    Last edited: Mar 4, 2014
  6. jgodfrey

    jgodfrey

    Joined:
    Nov 14, 2009
    Posts:
    564
    That doesn't look right. You're adding in the value of the 0'th element to the total, but then immediately zeroing the total. So, you'll end up with a total that doesn't include the value of the 0'th element. I assume that's not what you want, is it? I'm not sure what you're trying to do with the "if" statement. Maybe you're just trying to ensure that the total starts out as 0? In that case, just set it to zero before entering the loop...

    Jeff
     
  7. bugra271

    bugra271

    Joined:
    Feb 4, 2014
    Posts:
    17
    My opinion is just like your words. Resetting to immediately 0 would make some troubles with your project. I did it by mistake, but this is much more useful for my script :)