- Home /

# Multiple Floats sharing a range?

```
[Range(0,1)]
public float EnemyTilesPercent;
[Range(0,1)]
public float EventTilesPercent;
[Range(0,1)]
public float ItemTitlesPercent;
```

So basically what I am trying to do is populate a randomly generated game board with tiles. There are 5 different types of tiles, Enemy, Item, Event, Blank, and the starting tile. The starting tile is placed at the beginning, and the blank tiles just fill in the spaces that the other tiles don't use.

So basically there is 100% of the game board, and Enemy, Item, and Event tiles need to share that 100%. I want it so when in editor I increase one of them, if there isn't enough % left of the board it will decrease the other 2 to make room. Any thoughts?

I don't think [Range(a,b)] has any impact on the values at runtime, only limiting them in the inspector for human entry.

For your actual question, suppose:

```
a = 0.5
b = 0.2
c = 0.3
```

If you wanted to maintain the rule that these numbers sum to 1 and any changes made to one of these will preserve that rule **and** the ratio of the other 2 numbers, you have all of the information to do that algebra:

```
a = 0.8 // was 0.5
// We know the ratio between b and c
3b = 2c
b + c = 1 - a = 0.2
b = 0.2 - c
// We can just substitute now
3 * (0.2 - c) = 2c
c = 0.12
// And since we already have b in terms of c
b = 0.08
```

Replacing the fixed values here with your variables should be straightforward, but let me know if you need more help.

**Answer** by FuzzyLogic
·
Mar 01, 2018 at 12:01 AM

If you define an OnValidate() method in your monobehaviour, it will be called anytime a change is made in the inspector. Then you can perform validation on the values you are interested in.

You will need to keep track of the known values though, so you can detect which one of those variables has changed, otherwise you will have problems. The changed variable should be the dominant one. As in, the two variables that were NOT changed, should be adjusted so they all sum to the max range.

Since you want to sync more than 2 values, there is another issue that you should be aware of. If you change variable A in the editor to say 0.7, you will have to figure out how you will compensate in the other variables. Basically, you will have to split the difference. You can do something a little more fancy by splitting based on the weight of the existing values. Eg, B=0.5 and C=0.25 and you change A=0.7. You have to figure out how to adjust B and C to sum to 0.3. Since B is twice the size of C, you could set B=0.2 and C=0.1

basic example:

```
[Range(0,1)] public float A = 0.50f;
[Range(0,1)] public float B = 0.25f;
[Range(0,1)] public float C = 0.25f;
private float oldA, oldB, oldC;
private void OnValidate() {
// I didn't test these calculations. Take it as example only.
float remaining, ratio;
if (A != oldA) {
remaining = 1.0f - A;
ratio = B / C;
C = remaining / (1 + ratio);
B = remaining - C;
} else if (B != oldB) {
remaining = 1.0f - B;
ratio = A / C;
C = remaining / (1 + ratio);
A = remaining - C;
} else if (C != oldC) {
remaining = 1.0f - C;
ratio = B / A;
A = remaining / (1 + ratio);
B = remaining - A;
}
oldA = A;
oldB = B;
oldC = C;
}
```

To prime oldA, oldB and oldC for the first time, you should call your OnValidate() method from within your Start() or Awake() method.

```
void Start() {
OnValidate(); // sync the 'old' variables with the public ones for the first time
}
```

Actually using seperate values like this is not really a great way to specify percentage values. We once had a question for a radial multi value slider which would be the best way to specify multiple percentage values that should add up to 1.0. Though if an inspector solution is required it might be the best to let the user specify unbounded values and just calculate the percentages based on the total. So when you have 4 values like 1, 5, 4 and 6 you would get a total of "16" (1+5+4+6). By dividing each value by the total you get the percentages:

0.0625 ( 1/16)

0.3125 ( 5/16 )

0.25 ( 4/16)

0.375 ( 6/16 )

Those will add up to 1.0. It's usually a more natural way to specify "chances". The values just represent a relative likelyhood. So something with a value of 50 is twice as likely as something with a value of 25 and it's 10 times more likely than a values of 5 and 50 times more likely than a value of 1. The values don't really give a hint about the absolute percentage. However If you want to specify absolute percentages your solution is not very helpful. When you specify the percentage of the second value the percentage of the first value would change again. It's impossible to exactly specify each value since every change will adjust all other values.

Correct. I answered the question as the op wanted rather than what they needed. Partly to showcase the OnValidate() method, which is a very handy way of performing sanity checks on inspector input without having to write a custom inspector and partly to highlight the issues you mentioned. You should post your comment as an answer.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Major int interacting with lesser float 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

4.6 Slider round to int? 1 Answer

How do i make a float match to another float on other script 1 Answer