• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by jlowell000 · Sep 11, 2015 at 03:47 AM · boundslimitnumberbasicslimitations

Minimum value in public variables?

I have public integers that is controlling the size of a generated map. I don't want them to go below a particular value. Is there a way to limit the lower bound similar to [Range(0, 100)]?

edit_1: I could have the script force values lower to be the min, but I'd rather not. I feel like that would be lazier. The only other thing is I could catch it before (once there is more to the project) the script runs.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image getyour411 · Sep 11, 2015 at 03:49 AM 0
Share

Is Range not working?

http://answers.unity3d.com/questions/25074/whats-attribute-to-limit-range-of-serialized-varia.html

avatar image jlowell000 getyour411 · Sep 11, 2015 at 04:33 AM 0
Share

range take 2 argument min and max. I only need a min

avatar image Polymo jlowell000 · Sep 11, 2015 at 05:46 AM 1
Share

then just use the maxValue of the valueType as maximum

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Orami · Sep 11, 2015 at 08:36 AM

make a public int in the script called lowerBound use the inspector to set it to whatever value you want.

You could also make it private and set it in the script, but public would probably be easier for you to manipulate with the inspector.

Range(lowerBound, 100); //gives a number between lowerBound and 100 or if you want no limit use what ValooFX said and use MaxValue instead of 100;

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
8

Answer by booferei · Feb 09, 2017 at 02:05 PM

You can implement OnValidate and force the minimum. For example:

 void OnValidate() {
     distance = Mathf.Max(distance, 0);
 }

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gmkwak · Feb 13, 2018 at 05:54 AM 0
Share

This function is called when the script is loaded or a value is changed in the inspector (Called in the editor only).

this is what I really want!

avatar image mrpmorris · Mar 30, 2018 at 11:35 AM 0
Share

Excellent answer, thanks for $$anonymous$$ching me something new!

avatar image booferei · Jun 10, 2020 at 01:14 PM 0
Share

See Oferk99's new answer, which seems to supersede mine.

avatar image
4

Answer by Oferk99 · Jun 05, 2020 at 05:54 PM

Code:

 [Min(0)]
 public int example1;

 [Min(0f)]
 public float example2;

Code explanation: If you type (in inspector) something under the minimum, after you press enter it will set it back to the minimum. Or in this case - example1 & example2 minimum allowed input in inspector is 0.


WARNING - it only works in inspector, so if you were to set it in code to something under the minimum, it will stay under the minimum.

so in this case:

 void function()
 {
 example1 = -2;
 example2 = -2f;
 //It will stay -2, even though the minimum is 0
 }



Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How To Add A Limit To Amount Of Objects 1 Answer

How to access bounds of entity in ECS ? 1 Answer

How do I limit the speed of my car? 0 Answers

I cannot store data into PlayerPrefs after a certain amount of data 2 Answers

Limit number of builds running on one computer? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges