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.

1 Answer

1

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:

@bubzy You have a couple of other options to see variables in the inspector. - Turning on debug mode lets you see (read only) private fields in the inspector - Using [SerialiseField] private ... hooks the variable into Unity's serialisation system. This allows you to assign and edit the variable through the inspector, without the need to make it public That way you can keep public doing what it was designed for, fields that need to be accessed via other classes.

ooh, nice thanks