GUI Buttons to change values.

Hi. So I want to make clickalbe buttons that changes variables. I have following code:

var walkSpeed = PlatformerControllerMovement.walkSpeed;
var runSpeed = PlatformerControllerMovement.runSpeed;
var inAirControlAcceleration = PlatformerControllerMovement.runSpeed;
var gravity = PlatformerControllerMovement.gravity;
var maxFallSpeed = PlatformerControllerMovement.maxFallSpeed;
var speedSmooth = PlatformerControllerMovement.speedSmoothing;
var rotateSmooth = PlatformerControllerMovement.rotationSmoothing;

var height = PlatformerControllerJumping.height;
var extraHeight = PlatformerControllerJumping.extraHeight;
var doubleJumpHeight = PlatformerControllerJumping.doubleJumpHeight;

function OnGUI () {
    chooseButton(0,0,"walkSpeed", walkSpeed);
    chooseButton(1,0,"runSpeed", runSpeed);
    chooseButton(2,0,"inAirCtrlAcc", inAirControlAcceleration);
    chooseButton(3,0,"gravity", gravity);
    chooseButton(4,0,"maxFallSpeed", maxFallSpeed);
    chooseButton(5,0,"speedSmooth", speedSmooth);
    chooseButton(6,0,"rotateSmooth", rotateSmooth);

    chooseButton(0,1,"hate", height);
    chooseButton(1,1,"extraHate", extraHeight);
    chooseButton(2,1,"doubleHate", doubleJumpHeight);

function chooseButton (posX, posY, variableName, variableObj){

    GUI.Box(Rect (10+(posX*95),10+(posY*85),90,55), variableName);
    if (GUI.RepeatButton (Rect (25+(posX*95), 33+(posY*85), 25, 25), "-")) {
        variableObj -= 0.1;
    if (GUI.RepeatButton (Rect (60+(posX*95), 33+(posY*85), 25, 25), "+")) {
        variableObj += 0.1;

    GUI.Box(Rect (10+(posX*95),68+(posY*85),90,22), variableObj.ToString());

...and when i click button "-" or "+" nothing happens. I noticed, then if i will extend code from a function and type variables by hand - everything works! There is a webdemo, if somebody want to see it in action: click here.

Variables like ints and floats (which are passed by value) in functions are local to that function, so when you increase/decrease `variableName`, it changes it the function only, and the variable you passed in is not affected at all.

To get around that, you can affect the actual variables you're passing in by using reference variables instead of standard local variables. However, while you can use functions that have reference variables in Javascript, you can't make them. For that you need to use C# instead. It's very possible to make that function as a separate C# script, and keep the rest of the script in Javascript, if you're more comfortable with that.

There is a hackish way of doing this in Javascript if you really want. Since classes are always passed by reference and not by value, you can make a class for your variables (that would normally be passed by value) that you want to be directly affected by functions. The class can work basically the same way as a normal passed-by-value variable, but takes more work to set up and use.

Here's an example that makes a class called RefFloat, that is basically a float, except you can use it like a reference variable. The DoubleButton function will make a button that doubles the value of any RefFloats that are passed in.

class RefFloat {
    var value : float;
    function RefFloat (a : float) {
        value = a;

var var1 = new RefFloat(.33);
var var2 = new RefFloat(2.0);

function OnGUI () {
    DoubleButton("Variable 1", var1);
    DoubleButton("Variable 2", var2);

function DoubleButton (buttonLabel : String, variableName : RefFloat) {
    if (GUILayout.Button(buttonLabel + ": " + variableName.value.ToString())) {
        variableName.value *= 2;