- Home /
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
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;
}
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.
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!
Your answer worked perfectly. Try this before attempting any other solution.
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.
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 ;
}
}
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.
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.
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.
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.
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);
}
Your answer
Follow this Question
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