- Home /
Scaled Variables
Hi everyone,
I have a question about scaling variables to one and other.
If I was to have a set of variables inside of a scriptable object ie:
[Range(0,1)]
float a;
[Range(0,1)]
float b;
[Range(0,1)]
float c;
[Range(0,1)]
float d;
and so on, is there a way I can link them together so that the combined variables are used as a percentage of each out of a whole. So I could have all 5 as 0.2 and then adjust one to be 0.4 and the others would drop to 0.15 automatically.
Is this possible or is there a better option that someone else can see that I cannot.
Thank you!
Answer by FoundationGaming · Oct 15, 2021 at 01:32 AM
I've had to fully create a custom method and change the entire process. Will make things a lot more difficult further down the line, but it's the only way to keep going for now. Thank you for the input!
It would be nice of you to share some of your results with others that come seek the same answer
I ended up using a completely different method, completely dumping the idea of the variables being connected. If I get it working how I wanted originally then I will be sharing the results for sure.
Answer by Pangamini · Oct 14, 2021 at 10:36 PM
Implement the OnValidate() method. It gets called whenever you change the values in the inspector. You can normalize the values there. If all you want is to have the sum of them all to be 1, you could try
protected void OnValidate()
{
float sum = a + b+ c + d;
a /= sum;
b /= sum;
c /= sum;
d /= sum;
}
This might not be perfect, because it will also keep changing the value you are trying to set. You'd want to detect which one it is, and keep it out of the validation. I don't think you can detect that easily from OnValidate, but you could write a custom Editor / PropertyDrawer
Also, make sure the sum is never zero, or you will pierce the spacetime and go places
Unfortunately that only resulted in an "NaN" on all of the variables.
My code:
[Range(0, 1)]
public float percentIron = 1f; // To prevent dividing by ZERO
[Range(0, 1)]
public float percentCopper;
[Range(0, 1)]
public float percentCoal;
[Range(0, 1)]
public float percentGold;
[Range(0, 1)]
public float percentSilver;
[Range(0, 1)]
public float percentBauxite;
[Range(0, 1)]
public float percentSulfur;
[Range(0, 1)]
public float percentDiamond;
[Range(0, 1)]
public float percentTin;
[Range(0, 1)]
public float percent$$anonymous$$;
protected void OnValidate()
{
float sum = percentIron + percentCopper + percentCoal +percentGold + percentSilver +percentBauxite + percentSulfur + percentDiamond + percentTin + percent$$anonymous$$;
percentIron /= sum;
percentCopper /= sum;
percentCoal /= sum;
percentGold /= sum;
percentSilver /= sum;
percentBauxite /= sum;
percentSulfur /= sum;
percentDiamond /= sum;
percentTin /= sum;
percent$$anonymous$$ /= sum;
}
And I receive this:
Well, your sum was obviously zero at some point ;) I don't see how that initial value prevents anything
Your answer
Follow this Question
Related Questions
Multiple Floats sharing a range? 1 Answer
Changing float lenght 2 Answers
How do I add something on to a float. 1 Answer
Max scale in unity3d 2 Answers
3D knob to control range float 0 Answers