How to make a slider menu

I was wondering how to make a slider menu where you can scroll and when you let you it auto selects the closest option. I have searched around and none of the tutorials or codes I have found work properly or have this function. Does anybody know how to do this or where a good tutorial for this is? It would be very helpful.

using UnityEngine;
using System.Collections;

public class sliderThing: MonoBehaviour {

	// Use this for initialization

	public int menuOptions = 5;
	public int sliderSize = 100;
	public int menuDivisionSize;
	public int selectValue = 0;
	public int selectedOption;

	void Start () {
		menuDivisionSize = sliderSize/menuOptions;

	}
	
	// Update is called once per frame
	void Update () {
	
		if(Input.GetKey(KeyCode.D))
		{
			selectValue ++;
		}
		if(Input.GetKey(KeyCode.A))
		{
			selectValue --;
		}
		if(selectValue <0){selectValue = 0;}
		if(selectValue > sliderSize){selectValue = sliderSize;}
		for(int i = 0; i <= menuOptions; i++)
		{

			if(selectValue > i*menuDivisionSize && selectValue < (i+1)*menuDivisionSize)
			{
				selectedOption = i;
			}
		}
	}
}

this works, perhaps you can implement it into your game idea? variables are public because I like to see them in the editor for debugging :slight_smile: