Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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
3
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 $$anonymous$$ and max. I only need a $$anonymous$$

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

4 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
11

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 $$anonymous$$e.

avatar image
11

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
avatar image
0

Answer by RastaKillah · Oct 24, 2021 at 03:48 PM

BUT, if you want to set a limit to a number later, like "on the fly" , or just to convert:

 //If the number is less than the minimum amount, then return that, otherwise return itself
 numToMin = numToMin < minOf ? minOf : numToMin;
 //As well as clamp does the same, returning the float as maximum
 numToMin = Mathf.Clamp(numToMin, preferredMin, numToMin);

You can make a function from that, and probably you can also define a statement to a new temporary number with one line of code.

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

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.

Follow this Question

Answers Answers and Comments

10 People are following this question.

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

Related Questions

Limit number of bullet on screen c# 1 Answer

Is there a max number of lightmaps you can have? 1 Answer

Shooting with Limited Ammo 2 Answers

How to access bounds of entity in ECS ? 1 Answer

How To Add A Limit To Amount Of Objects 1 Answer


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