What attribute to limit range of serialized variable?

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?

You can also use the RangeAttribute in C# like this

[Range(1,6)]
public int WorkerThreads;

That will give you a slider in the Inspector that goes between the two values like this

13357-slider.png

The hard-to-find manual entry is here:

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:

	[ExecuteInEditMode]
	void OnValidate(){
		labelPosition.x = Mathf.Clamp01(labelPosition.x);
		labelPosition.y = Mathf.Clamp01(labelPosition.y);
	}

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.

You're probably looking for Mathf.Clamp.

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