- Home /
how subtract total value element of array ??
hey everyone, i want to subtract element value of array
 public int[] meatBallValue;
  public int subtractMeatBall;
  public int totalValue;
      private void Start()
      {
          meatBallValue[0] = 7;
          meatBallValue[1] = 4;
          subtractMeatBall = 10;
      }
  
      private void Update()
      {
                  totalValue = meatBallValue[0] + meatBallValue[1];
               //so totalValue = 11
  
          if (Input.GetKeyDown(KeyCode.Space))
          {
              //meatBallValue[0 && 1] -= subtractMeatBall;
              //how can i to subtract meatBallValue ??
              //but i don't want to subtract totalValue, because totalValue is just showing the total value element of array (meatBallValue)
              //i want to subtract meatBallValue[] because from this the real value take of
          }
      }
thanks,,
Answer by Vice_Versa · Aug 14, 2018 at 01:09 AM
Holy Shit is this a game about spaghetti and meatballs???? THATS THE MOST AWESOME THING IVE HEARD ALL WEEK!!!!! anyway, I dont quite understand what you are trying to do? It looks like you are trying to have a total of all the meatballs, then subtract a certain amount from the total? I dont think an array would necessarily be the right way of doing this but screw it lets stick with it
 totalValue = meatBallValue[0] + meatBallValue[1];
 float tempNumber = subtractMeatBall;
 if (Input.GetKeyDown(KeyCode.Space))
 {
   //im assuming we'll be going with First in First Out?
   for (int i = 0; i < meatBallValue.Length; i++)
   {
     if (tempNumber > meatBallValue[i])
     {
        tempNumber -= metalBallValue[i];
       meatBallValue[i] = 0;
     }
     else
     {
       meatBallValue[i] -= tempValue;
       break;
     }
   }
 }
the reason I dont recommend using an array for this is because now the first thing in the totalMeatballs array has 0 meatballs, youll have to check to see if there are 0 meatballs in each element when adding more or else youll have tons of elements with 0 meatballs
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                