[Easy] Constant variable depending on other variable (a = a + b)

Hello, currently when we write in JS a line for Update function including var1 = var1 + var2 it will loop adding var2 to var1. How can I make it always constant like when 10 = 10 + 1 than it is 11 and does not continue adding and go 12,13,14 etc…? I hope you can understand me, thank you.

you can have a boolean added and then do:
if(!added){
var1 = var1 + var2;
added = true;
}

I’ve made that and it doesn’t work as I want it to. It is adding value when it changes, it is supposed to make var1 + var2 for all the time.

How it works now:
var1 = 10;
1=1+var1 shows 11 than var1 changes to 5 and it shows 16
How it should work:
1=1+var1 shows 11 than var1 changes to 5 it has to show 6

var distance : float = 0;
private var maxpitch : float = 1.1;
private var minpitch : float = 0.9;
private var added : boolean = false;
private var distance1 : float = 0;

function Update () {
if (distance1 != distance) {
added = false;
}
if (added == false) {
maxpitch = maxpitch + distance;
minpitch = minpitch - distance;
distance1 = distance;
added = true;
}
}

As soon as you set distance1 = distance; in the very next update the first if statement will see those two variables equals and will set again the added bool to false, which will cause the execution of the second if statement again and again, ignoring thus your request to stop the increasing addition.

Would you please explain exactly your need to set distance1 = distance; ? And in general, what are you trying to do ?

btw the easiest solution to this would be to create another variable and use it instead of incrementing the original one

     float j;
     float k;
     float r;

    void Update()
        {
              r = j+k;    // instead of k = k + j;
        }

Don’t care about the code I’ve made, its difficult to explain but I want it to work like this:

How it works now:
var1 = 1 and var2 = 10

var1 = var1 + var2 shows 11 than var2 changes to 5 and it shows 16

How it should work:

var1 = var1 + var2 shows 11 than var2 changes to 5 it should show 6

I hope you get the idea, and I prefer to use Javascript if possible.