Search Unity

MinAttribute not working

Discussion in 'Scripting' started by Giustitia, Aug 11, 2019.

  1. Giustitia

    Giustitia

    Joined:
    Oct 26, 2015
    Posts:
    113
    Hi guys!

    I'don't know if it's me or is a known bug, but UnityEngine.MinAttribute is not working for me on float and int variables. I have a variable declared like this:

    [MinAttribute(1)]
    public float something;

    Then in insepctor I'm able to set this value under 1, even by typing, scrolling or whatever. I'm using Unity 2019.2 atm.

    Thanks for any reply! :)
     
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    It's essentially a property drawer for numerical types as I understand it, so it's worth noting that it's not a validation attribute and won't stop the value from being under a certain amount programmatically, but rather just makes a visual restriction in the inspector. It won't apply if the inspector is in Debug mode (because that bypasses the editor script that applies property drawers entirely), and it won't apply if you're using a custom inspector (in some cases) or another property drawer for that property which isn't taking the Min attribute into account and takes precedence.
     
    Joe-Censored likes this.
  3. Giustitia

    Giustitia

    Joined:
    Oct 26, 2015
    Posts:
    113
    Hi Lysander, thanks for the answer!

    I'm not changing it via code, either in debug mode or custom inspector, just typing in its field via normal Inspector or dragging the field. Others attributes like [Range] are working properly.
     
  4. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    You may not be referencing the MinAttribute that you think you are- be sure to check your namespaces and be sure that the UnityEngine.MinAttribute is the one being used. PostProcessing also (IIRC) has a MinAttribute which may interfere with this behaviour. Depending on the version of PostProcessing you have in your project, it may be worth looking into PostProcessing.MinDrawer to see if there's a conflict there as well.
     
  5. Giustitia

    Giustitia

    Joined:
    Oct 26, 2015
    Posts:
    113
    Also checked, either having "using UnityEngine" as declaring the attribute like UnityEngine.MinAttribute :/
    It smells like a bug like spirit
     
  6. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    It does appear to be a bug- the associated PropertyDrawer class should theoretically be in this script, but as you can see it's absent. My advice is to use Range instead- if you need to, you can use int.MaxValue as the upper limit, but the drag increments are pretty massive at that level, and it doesn't protect against typed-in values. You can report it if you like- I couldn't find an existing report for it, but I didn't look that hard either.

    Alternatively, you can copy the RangeDrawer in that script I linked and paste it in your own file, change the type it draws for to the MinAttribute, and adjust it to look like whatever you want. =)
     
    Last edited: Aug 11, 2019
  7. ahmafi

    ahmafi

    Joined:
    Feb 28, 2019
    Posts:
    9
    I have the same problem, it seems to be a bug. I also checked everything that Lysander said. Min() doesn't work at all.
     
  8. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    You should report it as a bug!
     
    richardkettlewell likes this.
  9. ahmafi

    ahmafi

    Joined:
    Feb 28, 2019
    Posts:
    9
    I reported this problem after I read this thread.
     
    hadynlander likes this.
  10. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    We are fixing this! Thanks for the report!
     
    Rabadash8820 and ahmafi like this.
  11. ahmafi

    ahmafi

    Joined:
    Feb 28, 2019
    Posts:
    9
    (Case 1183703) It's now fixed in Unity 2020.1.0a7 and above.
     
  12. Hubertsn

    Hubertsn

    Joined:
    Aug 7, 2015
    Posts:
    9
    This won't be fixed in Unity 2018.4 LTS and 2019?
     
  13. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285