When editing a scripts variable in another script, does the original script update?

If you pull a number variable from a script, and in another script, say subtract amount from variable, will that number be updated in the original script?

To go in more depth, I have a int variable from script1. I am subtracting from that variable in script2. So, how do I make script1 updated to the new int? The variable from script1 is defined as a new variable in script2 if that matters.

Hey there, bpears. There are two ways to get a variable from another location – by reference and by value. When you take a variable from a script1 normally (by reference), without using the “ref” keyword, you get a copy that has no effect on the original variable. But if you use the ref keyword, the variable’s location in memory will be passed as well – so the original value of the int variable in script1 can be changed in script2.

This should explain the syntax:

If you want to change a variable in another script, just change it: cowScript.x = cowScript.x + 2;

If you copy it, then changing the copy won’t do anything to the original. But, a common trick to save typing is to copy, change and copy back:

x2 = cowScript.x;
if(x2<5) .... x2+=6; if(x2>10) .... // don't have to write cowScript.x so much
cowScript.x = x2; // copy back when done

But, some variables are actually just links (pointers.) For example, transforms. If you copy a transform, you don’t really. You just get another link to the original transform (yes, this is a very confusing rule.) So this works:

var tt : Transform = cowScript.tail;
tt.localPosition = Vector3.zero;
// same as cowScript.tail.localPosition = Vector3.zero;