Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

why is this adding values instead of multiplying?

Discussion in 'Getting Started' started by Sylvir, May 4, 2015.

  1. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    Code (CSharp):
    1.     public float GetFishPerSec()
    2.     {
    3.         float tick2 = 0;
    4.         float tick1 = 0;
    5.         float tick = 0;
    6.         foreach(float item in tickValue)
    7.         {
    8.             tick += idleCount[0] * tickValue[0];
    9.             tick1 += idleCount[1] * tickValue[1];
    10.             tick2 += idleCount[2] * tickValue[2];
    11.         }
    12.         return tick + tick1 + tick2;  
    13.     }
    14.  
    been trying to work this out for the last couple hours, and i am not understanding why this is taking the idlecount value and the tickvalue and adding them together instead of mulitplying it.

    Code (CSharp):
    1.         tickValue [0] = 0.2f;
    2.         tickValue [1] = 0.7f;
    3.         tickValue [2] = 1.5f;
    4.         tickValue [3] = 3;
    5.         tickValue [4] = 5.5f;
    6.         tickValue [5] = 7;
    7.    
    8.         idleCost [0] = 500;
    9.         idleCost [1] = 1200;
    10.         idleCost [2] = 5000;
    11.         idleCost [3] = 150000;
    12.         idleCost [4] = 500000;
    13.         idleCost [5] = 1000000;
    14.    
    15.         idleCount [0] = 0;
    16.         idleCount [1] = 0;
    17.         idleCount [2] = 0;
    18.         idleCount [3] = 0;
    19.         idleCount [4] = 0;
    20.         idleCount [5] = 0;

    as an example when i get 1 count of the [0] part of the array it goes 1 + 0.2 instead of 1 times 0.2, can anyone tell me why this is not multiplying? Thank you.
     
  2. proandrius

    proandrius

    Unity Technologies

    Joined:
    Dec 4, 2012
    Posts:
    544
    Firstly your all idleCount array is 0, so it is always multiplying by 0. Secondly when doing foreach you need to use item instead of tickValue. But in general I don't really understand what are you trying to achieve here. :)
     
  3. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    the 0s are all there as the starting point for each one. as soon as you purchase something it ups to 1 at the min, and then does the math. thanks the item instead of tickvalue is probably the issue. Thank you! thought you used the array name there
     
  4. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    So it should be float item in item for my foreach?
     
  5. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    the results I am getting currently is its adding the 1 count to the 0.2 tick value to get 1.2 instead of multiplying it to get 0.2 then 0.4 with a 2nd count and so on
     
  6. narf03

    narf03

    Joined:
    Aug 11, 2014
    Posts:
    222
    LoL how about u tell us what do you want to achieve ? We cant help you if we dont understand what do you want. Like using your GPS, u need to know your destination coordinate, keep driving without a direction wont help.
     
    blizzy and Kiwasi like this.