I have a serialized public variable. I want to limit its value within 0 - 1 in inspector.
I’ve seen that the character controller component has this functionality. I tried searching in the manual but I couldn’t find how to do it. What’s attribute or how can I limit the value range of serialized variable in inspector?
I know this is a dead issue, but to the benefit of future googlers like myself, I want to post this useful bit of code. It is similar to the accepted answer, but the range checking code only runs when the value is actually changed:
To get an editor in the inpector to clamp your values, you can use either a slider as is done for shaders or you would have to clamp the value silently as is done for dynamic friction. To create a slider which clamps your values, you would have to implement a CustomEditor for your class and create a EditorGUI.MinMaxSlider. To silently clamp your values, you would do what Jashan suggested. If you were to make a CustomEditor, even for these silently clamped values, you could add a tooltip to indicate your clamping.
If you use that in Update() (and to be super-safe also in Awake() and Start()), you're value will be clamped all the time (even if you try to store the value in the editor). You might also want to use ExecuteInEditMode