- Home /
(Logic) Switch GameObjects With Slider Using Arrays
Hello, I am working on a school project which I am planning to show it on an Android tablet with touch input & gestures. And I am stuck at the very beginning.
Lets say I have 5 flats in a building on top of each other, named Flat0 to Flat4. I want to cycle through them with user input using Slider (any other idea is welcome!)
If slider value is 4(top value), it will show top flat with others active. If below 4, it will hide above current slider value.
So far I managed to hide and show game objects using arrays, but they don't work as they should. It seems like one of the lists start from 1 to 5 and my arrays work 0 to 4, for sure. Yet, I couldn't find the problem.
Thanks so much for the help!
Here is the code I managed to do so far:
function sliderChange(){
//Slider Gets the current slider value
var Slider : UnityEngine.UI.Slider = GameObject.Find("slider").GetComponent(UnityEngine.UI.Slider);
var currentSliderValue = Slider.value;
//Slider Text: Writes the flat number to UI Text
var SliderText : UI.Text = GameObject.Find("sliderText").GetComponent(UI.Text);
SliderText.text = "Flat: " + currentSliderValue;
var flatNumber = GameObject.FindGameObjectsWithTag("Flat") as GameObject[];
//On slider change displays the first state
Debug.LogError("Current Slider Value: " + currentSliderValue + " Flat Number: " + flatNumber.length);
//HIDE
var j = currentSliderValue;
while(flatNumber.length > j){
flatNumber[j].GetComponent(MeshRenderer).enabled = false;
Debug.LogWarning("Hide: " + j);
j++;
}
//SHOW
var k = currentSliderValue;
while(0 <= k){
flatNumber[k].GetComponent(MeshRenderer).enabled = true;
Debug.Log("Show: " + k);
k--;
}
}
Okay, I reimported the meshes one by one. I was afraid of the script getting array in a random order. This time everything changed, still won't work but it seems I need to order arrays alphabetically. Is this the problem? Or why it gets elements randomly?