Use Another Float Value As Range Attribute's Max Value

Hi guys,

I want to use a float value to define the max range of the Range attribute.

For example,

float a = 4.0f;
[Range(0.1f, a)]
public float b;

Is this possible?

Thanks!

You can do this by using the “const” keyword in the “a” field.

 const float a = 4.0f;
 [Range(0.1f, a)]
 public float b;

Since attribute parameters must be constant by default you cannot pass changing variables as parameters. But you can insert your own logic as function using the tool ‘Custom Inspector’ (Custom Inspector | Utilities Tools | Unity Asset Store) from the asset store. You can do testing or changing directly with it.

  • Test:
    This code would test if a is greater than b and show an error in the inspector if a is not greater

    [Validate(nameof(AIsGreaterThanB), “has to be greater than B”)] public int a;
    public int b;
    bool AIsGreaterThanB() { return a > b; }
    preview of the inspector:

204162-unbenannt.png

  • Changing directly (would be like the max attribute)
    the hook attribute gets called on every change in the inspector

    [Hook(nameof(MakeAGreaterB))] public int a;
    [Hook(nameof(MakeAGreaterB))] public int b;
    void MakeAGreaterB() { if (a < b) { a = b; } }