- Home /
Use Slider for Geometry (GameObject) 'layers'
Hi,
I've been trying to find out a way or if there is a way of using a slider UI to activate layers, for example, I have 5 cubes one on top of each other, and I would like to use a slider to 'activate' (show/hide) from the first to the fifth in order. (slider to the left only shows the first cube, the slider in the middle shows 3 cubes and the slider to the right shows all five of them)
Is it possible to do this with a slider, making sure the first cube is always visible?
Thank you for any help in advance,
Best Regards!
Answer by yummy81 · Feb 13, 2018 at 05:49 PM
So, create an empty gameobject. I named it "SanctiMortem". Next, create the slider and five cubes, and place them as you wish at the scene. Finally, add the script to the "SanctiMortem" gameobject and drop the slider and five cubes into appropriate slots in the inspector. This is how my scene looks like and your script:
using UnityEngine;
public class SanctiMortem : MonoBehaviour
{
public UnityEngine.UI.Slider slider;
public GameObject[] cubes;
private int minValue, maxValue;
private void Awake()
{
if (cubes.Length == 0) return;
slider.wholeNumbers = true;
slider.minValue = 1;
slider.maxValue = cubes.Length;
minValue = (int) slider.minValue;
maxValue = (int) slider.maxValue;
slider.value = 1;
for (int i = minValue; i <= maxValue; i++) cubes[i-1].SetActive(false);
}
private void Update()
{
if (cubes.Length == 0) return;
for (int i = minValue; i <= maxValue; i++)
{
if ( i <= (int) slider.value )
cubes[i-1].SetActive(true);
else
cubes[i-1].SetActive(false);
}
Debug.Log("Slider value: " + (int) slider.value);
}
}
Thank you so $$anonymous$$uch!
Worked perfectly!
Best regards!!