Problem with Array

why does the switch-case work but the array doesnt (marked as comment (ignore second comment line))
113193-f5673a2e29dda61d200a4c28df53e2d7.png

A float variable is a valuetype. Valuetypes can’t be “referenced”. When you create your arrays:

float[] dirts = {dirt0, dirt1};

You actually copy the values of dirt0 and dirt1 into the array. Inside your loop you just change the values inside the array. You can never store a reference to a variable in another variable, even for reference types. When you deal with reference types the actual variable contains a reference to an object. When you copy the “value” of such a variable into another you actually copy the reference to the object so both variables would reference the same object.

Though as i said this doesn’t work with value types. If you have many values you should have used arrays in the first place. If you want to keep the variables seperate you can replace all your code with just

dirt0 = (dirt0>washperSec0)?dirt0 - washperSec0: dirt0;
dirt1 = (dirt1>washperSec1)?dirt1 - washperSec1: dirt1;
// [ ... ]

Or if you don’t like the ternary operator a more clear version would be to create a method for the logic

float SubtractIfGreater(float val, float sub)
{
    if (val > sub)
        return val - sub;
    return sub;
}

public void Wash()
{
    dirt0 = SubtractIfGreater(dirt0, washperSec0);
    dirt1 = SubtractIfGreater(dirt1, washperSec1);
    // [ ... ]
}

However if you have more than 2 such variable “pairs” you probably want to store then in an array anyways.

Thank you very much!