linking floats

I need two floats to be linked, so when I change one, the other changes with it. This needs to be able to go either way.

For example:

var example1 : float;
var example2 : float;
        
function Update ()
{
   example1 = example2;
   example2 = example1;
}

If I change example2, example1 changes with it, but if I try to change example1, it doesn’t work.

Ummm.... why?

Also with tamoshimi, but just for fun... ;) void Start () { float x = 10; refs(ref x, ref x); } void refs(ref float X, ref float y) { X = 10; y = 11; Debug.Log("x: " + X);//outputs 11 X = 12; Debug.Log("y: " + y);//outputs 12 }

Why? In my settings you can choose whether the sensitivity of the mouse axes are to be seperate, if not, I would like to be able to keep the same gui and mechanics, but have them stay the same.

2 Answers

2

I’m with tamoshimi, why?.. but if you still wanted too you could use property setter / getters…

eg.

private int _example1and2 = 1;
private int example1 { get { return _example1and2; } set { _example1and2 = value; } }
private int example2 { get { return _example1and2; } set { _example1and2 = value; } }

//both example 1 & 2 = 1

example1 = 4;

//both example 1 & 2 = 4

example2 = 7;

//both example 1 & 2 = 7

Its worth noting that properties are full blown methods. So this solution could easily extend out for your use case.

I don’t know how you set your variables right now but I’ll just guess you’re using some kind of slider and a checkbox which sets a boolean. If you’re set up like this then it’s pretty straightforward.

var keep_values_same : bool;
var axis_x : float;
var axis_y : float;

function set_axis_x(new_value: float){
	axis_x = new_value;
	if(keep_values_same) axis_y = axis_x;
}

function set_axis_y(new_value: float){
	axis_y = new_value;
	if(keep_values_same) axis_x = axis_y;
}

Instead of changing the values of axis_x and axis_y directly you’ll now have to call set_axis_x and set_axis_y instead. This’ll make sure the other value changes aswell if your keep_values_same boolen is set to true.