# Is there an attribute for numbers (floats and ints) to skip on increments?

Say I wanted my number to skip from 0 to 16 to 32 depending on what number is closer to the one I feed it. How? Is there an attribute?

Mathf.Round possibly? Depending on what you actually want to do with it… Mathf.Round(int * 8) / 8 possibly?

There is no attribute, but the formula is fairly straightforward.

``````float increment = 16;
value = Mathf.RoundToInt(value / increment) * increment;
``````

The idea is to divide the value by the increment you want. This gives you the number those increments that are in the value. You then round the result to the nearest integer. Multiplying the rounded result by increment gives you the final value to the nearest increment.

You can make this a function and pass in the value and increment…

``````float RoundToNearest(float value, float increment)
{
return Mathf.RoundToInt(value / increment) * increment;
}
``````

I’m, uh, 96% sure that works fine with any floating point increment value. But test, test, test…

You would surely do this as an extension, so roughly

``````using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public static class Extns
{

public static float ToSixteens(this float f)
{
return Mathf.RoundToInt(f / 16f) * 16f;
}

}
``````

If you’re just learning to program it’s absolutely essential you get with extensions (aka categories in other languages).

Google up some tutorials example Extension Methods - The complete C# tutorial

since you asked for an attribute, you imply that the variable should clamp already in the inspector and not in some runtime routine.
I would recommend custom editors. It would look like this:

in code:

-> in inspector you cannot anymore insert values that are not multiple of 16

You could make them by yourself or use pre-programmed ones from the asset store. I used ‘Custom Inspector’: Custom Inspector | Utilities Tools | Unity Asset Store