Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
4
Question by reptilebeats · May 13, 2012 at 01:59 AM · floatintvarible

setting max varible value

i was just wandering how you make a maximum varible float number in unity. not during runtime but in editor, so basically i could set a max and min to 1,10 and i would not be able to go past that in the inspector

cheers

Comment
Add comment
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

5 Replies

· Add your reply
  • Sort: 
avatar image
9

Answer by AE40 · Feb 12, 2016 at 05:58 PM

@reptilebeats you can use the prefix [Range(numMin, numMax)] before the variable like this:

using UnityEngine; using System.Collections;

public class FightingScript : MonoBehaviour {

 [Range(0, 1)]public float health;
 [Range(0, 100)]public int minDamage;
 [Range(0, 100)]public int maxDamage;

}

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 MatthewRowe · Mar 15, 2020 at 12:42 PM 0
Share

This is a very good solution, just a note..

I found if you set a default value it must be "within" the upper and lower range so [Range(0, 1)]public float health = 1; //This won't work. It needs to be "within 0..1" so... [Range(0, 1)]public float health = 0.9; //This will work.

avatar image
4

Answer by mysteriosum · Mar 28, 2013 at 03:32 PM

I found a good hack for this: if you put the Mathf.Clamp() function in the OnDrawGizmos override, it'll get called in the editor without having to mess with the rest of your script. It works great for me!

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 cbarba · Oct 17, 2013 at 02:31 PM 1
Share

Your answer worked perfectly. Try this before attempting any other solution.

avatar image
3

Answer by Samuel · May 13, 2012 at 05:32 AM

according to: http://answers.unity3d.com/questions/186832/slider-bar-in-inspector.html

You can try to extend the editor.

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
2

Answer by Fr0stbite · Oct 17, 2013 at 02:51 PM

i use this:

 var myFloat : float;
 var maxFloatNum : float;
 function Update () {
 if(myFloat > maxFloatNum){  // Replace maxFloatNum with any number
 myFloat = maxFloatNum ;
 }
 }
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
0

Answer by Noah-1 · May 13, 2012 at 02:01 AM

Look up for Mathf.Camp, that should make it.

This has been asked many times: Example. Please google it next time.

Comment
Add comment · Show 7 · 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 Lo0NuhtiK · May 13, 2012 at 02:06 AM 1
Share

Looks like they're asking about inspector-specific clamping, not only inside the script. To where if you try to change something clamped between 0 and 5 in code, to 532340934 in the inspector it won't allow it.

avatar image reptilebeats · May 13, 2012 at 02:10 AM 0
Share

yes im just asking about the inspector clamping, i dont have much knowledge of making scripts outside of runtime if you know what i mean.

i can set max and $$anonymous$$s when game is running but would like to be able to do it when game isnt running for my own benifit.

avatar image Lo0NuhtiK · May 13, 2012 at 02:30 AM 0
Share

You could still do it with clamp and having your script run in edit mode. Only thing that sucks about that is it will run all the other stuff in your code also.

avatar image reptilebeats · May 13, 2012 at 02:55 AM 0
Share

yep just fond that edit line, shame it cant be set as a function, i just tried mathf but wouldnt work just set it to the first number. when i set it in if's it works.

i might be using it wrong i never use math operations, i had something like

pragma strict

script ExecuteInEdit$$anonymous$$ode()

var alphaAmount : float;

function OnGUI(){

alphaAmount = $$anonymous$$athf.Clamp(1,0.05,0.5);

}

avatar image reptilebeats · May 13, 2012 at 03:08 AM 0
Share

doesnt matter had to put alpha in as my value

Show more comments

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

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

Related Questions

A node in a childnode? 1 Answer

Primitive data types (for code, not modeling) 2 Answers

int conversion gives bad results ! 3 Answers

Convert float to int without Math.Round? 2 Answers

What is wrong with my throttle script? 2 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