Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[Range(0, int.MaxValue)] limiting to Int32.MinValue?!

Discussion in 'Scripting' started by CoolJosh3k, Apr 30, 2020.

  1. CoolJosh3k

    CoolJosh3k

    Joined:
    Dec 3, 2016
    Posts:
    145
    Not sure if I am overlooking something or if this is a bug, but when trying to limit a variable range I end up with unexpected results.

    By using

    [Range(1, uint.MaxValue)] public int MyVar = 1;

    My slider goes from 0 backwards through to -2147483648.

    I have the same result if I use

    [Range(0, int.MaxValue)] public int MyVar = 1;

    , but this time I also notice that the little nob dissapears on the slider when reset to the default value (of 1);

    At the very least I am curious as to why it would end up being Int32.MinValue.

    Using Version 2019.3.11f1
     
  2. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    the first one is clear:
    uint.MaxValue
    is way higher than
    int.MaxValue
    . It is the binary value of
    1111 1111  1111 1111  1111 1111  1111 1111
    this is the value of
    int.MinValue
    .
    int.MaxValue
    would be
    0111 1111  1111 1111  1111 1111  1111 1111
    (the first bit decides if it is negative or positive).

    The second problem is not so easy to understand. Maybe there are some operations in the RangeAttribute which would lead to an overflow of the value.
    Try
    [Range(0, int.MaxValue - 1)]
    if that is high enough for you.
     
  3. CoolJosh3k

    CoolJosh3k

    Joined:
    Dec 3, 2016
    Posts:
    145
    I figured that perhaps anything above Int32.MaxValue was not supported, which is why I tried using int.MaxValue instead.

    I did think of the whole issue of a bug where even int.MaxValue was too large so I had already tried (int.MaxValue - 1) and strangly that too does not fix the issue. I still end up with it going to Int32.MinValue.

    I can get around it since I will never need a value that large, but it upsets me having to hardcode a number in there.

    Since it is possible for the inspector to work with uint I can enter up to the 4294967295 limit if not using a range, I figure there is perhaps a bug with how the Unity's Range function works.