- Home /
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.
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.
Answer by KayelGee · Oct 15, 2014 at 08:25 AM
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.
Answer by KpjComp · Oct 14, 2014 at 06:49 PM
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.
Your answer
Follow this Question
Related Questions
Operator cannot be used with left hand side of type.... 1 Answer
Where can I submit a unity designed website 0 Answers
Deep Linking to specific Scene in Android build 0 Answers
Error: "ArgumentException: get_deltaTime can only be called from the main thread" 1 Answer
Open link in new tab from webplayer hosted on any site. 3 Answers