2
[Range(-3, 3)]
public float range;

I want later in the code to do something like :

range.min = 2 
range.max = 20;

Or get

int min = range.min;
int max = range.max;

2 Answers2

4

You can't change an attribute's state at runtime, but you can read it using reflection:

class YourClass
{
    [Range(-3, 3)]
    public float range;
}

var range = typeof(YourClass)
    .GetField(nameof(YourClass.range))
    .GetCustomAttribute<RangeAttribute>();

float min = range.min;
float max = range.max;

Based on: https://github.com/jamesjlinden/unity-decompiled/blob/master/UnityEngine/UnityEngine/RangeAttribute.cs

Johnathan Barclay
  • 18,599
  • 1
  • 22
  • 35
  • Afaik it is `min` and `max` – derHugo Jun 23 '20 at 13:28
  • @derHugo `max` would be the same principle. I've updated the answer for clarity. – Johnathan Barclay Jun 23 '20 at 13:39
  • What I ment is afaik it is not called `Minimum` and `Maximum` but the fields are `min` and `max` – derHugo Jun 23 '20 at 13:52
  • @derHugo No, that's not the case: https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.rangeattribute?view=netcore-3.1#properties. – Johnathan Barclay Jun 23 '20 at 14:05
  • hm I was more thinking about the Unity adoption [`UnityEngine.RangeAttribute`](https://docs.unity3d.com/ScriptReference/RangeAttribute.html) (see [Unity Decompiled / RangeAttribute.cs](https://github.com/jamesjlinden/unity-decompiled/blob/master/UnityEngine/UnityEngine/RangeAttribute.cs)) since OP tagged Unity3D it is very likely OP is using the UnityEngine version ;) – derHugo Jun 23 '20 at 14:07
  • derHugo the fastest unity ninja in town, yes, it is better this way thank you – zambari Jun 25 '20 at 02:24
-1

Can you change it to an object like below?

public class range
{
    private float _value;
    public range(int min, int max)
    {
        Min = min;
        Max = max;
    }


    public float Value
    {
        get
        {
            return _value;
        }
        set
        {
            if (value > Max || value < Min) throw new Exception("value out of 
               range.");
            _value = value;
        }
    }

    public int Min { get; }

    public int Max { get; }

}

use it like;

rangeObj = new range(-3,3);
rangeObj.Min
rangeObj.Max
rangeObj.Value
Jonny Boy
  • 192
  • 1
  • 1
  • 10
  • your class should probably be `public` or at least `[Serializable]` otherwise it won't even show up in the Inspector – derHugo Jun 23 '20 at 14:02
  • @derHugo - class should be public - I was thinking it is implicit. I will fix it. – Jonny Boy Jun 23 '20 at 14:03
  • it still needs to be `[Serializable]` though ;) – derHugo Jun 23 '20 at 14:05
  • 1
    This won't compile. Attribute arguments bust be constant at compile time. – Johnathan Barclay Jun 23 '20 at 14:07
  • true that .. didn't even notice your are still using it as attribute. It also won't work as intended since Unity doesn't serialize any properties ... only fields. – derHugo Jun 23 '20 at 14:12
  • @derHugo - my bad missed that - I did not use VS to write that. See the edited version if that works. – Jonny Boy Jun 23 '20 at 14:27
  • Instead of an exception you should probably rather use `Mathf.Clamp` to force the value within the range .. also as said not sure if this is still what OP wants to achieve as this now doesn't use any special property drawer for the Inspector – derHugo Jun 23 '20 at 14:34
  • @derHugo That is on the user's discretion, I will leave that handling on the user. Enforcing a value on error apart from null is not good practice. – Jonny Boy Jun 23 '20 at 14:50