Hi
I am getting a group of gameobjects and need to get the values the user sets for sliders and toggles.
I haven’t got very far and need some help if possible, please!
var objects = GameObject.FindGameObjectsWithTag("formfield");
Debug.LogWarning("how many? " + objects.Length);
foreach (var obj5 in objects)
{
Debug.Log("object name:" + obj5.name); //this works a treat
// int valueBool = Convert.ToInt32(obj5.isOn); //this doesn't. How do i get the value of the toggle or slider.
}
I have done numerous searches and can’t find anything; but think that is probably because I don’t know how to encapsulate this question in a search!
Since you need the sliders and toggles, find the sliders and toggle instead of the GameObjects… Sincerely not sure which of these would have a better performance but you could:
var selectables = GameObject.FindObjectsOfType<Selectable>();
for(int i = 0; i < selectables.Length; i++)
{
if(selectables[i] is Toggle)
{
(selectables[i] as Toggle).//Your action
return;
}
if(selectables[i] is Slider)
{
(selectables[i] as Slider).//Your action
}
}
var toggles = GameObject.FindObjectsOfType<Toggle>();
var sliders = GameObject.FindObjectsOfType<Slider>();
for(int i = 0; i < toggles.Length; i++)
{
}
for(int i = 0; i < sliders.Length; i++)
{
}
Anyway, if it’s posibble to have the sliders serialized or stored at any variable at runtime without using FindObject methods it would be better.
I won’t know what they are as have multiple scenes with different numbers of sliders and toggles. If I serialise won’t I have to hard code? My aim is to do this dynamically as different scenes have different make up of sliders and toggles and don’t want a script for each scene
Will try your solution - thanks very much for helping me out
ok so to wrap up this post, this is what I ended up with
To get values from DB and populate scene:
//get the results ffrom your db and return it to the c# script
string json = wwwa.downloadHandler.text;
UserHabits loadedPlayerData = JsonUtility.FromJson<UserHabits>(json);
var toggles = GameObject.FindObjectsOfType<Toggle>();
var sliders = GameObject.FindObjectsOfType<Slider>();
Debug.Log("111. record count: " + loadedPlayerData.data.Count);
//sliders
foreach (var sliderIs in sliders)
{
var slidervalue = sliderIs.value;
var nameofslider = sliderIs.name;
int numbernameofslider = Convert.ToInt32(nameofslider);
for (int i = 0; i < loadedPlayerData.data.Count; i++)
{
if (loadedPlayerData.data[i].Habit_ID == numbernameofslider)
{
sliderIs.value = loadedPlayerData.data[i].amount;
}
}
}
//then get the values from the scene and write back to DB:
var toggles2 = GameObject.FindObjectsOfType<Toggle>();
var sliders2 = GameObject.FindObjectsOfType<Slider>();
//get the values of each slider
//get the label for each slider
//set the obj to loop until count is finished
//set the the global variable for next scene
UserHabitsPut obj = new UserHabitsPut();
foreach (var sliderIs2 in sliders2)
{
var habitValue = sliderIs2.value;
habitValue_U = Convert.ToInt32(habitValue);
var habitID = sliderIs2.name;
habitID_U = Convert.ToInt32(habitID);
//add to JSON
obj.data1.Add(new habitinfoput()
{
Habit_ID = habitID_U,
amount = habitValue_U,
label = "slider"
});
}
This required me to change the names of the form controls to correspond to the DB ID. I imagine there is a way to assign a variable to the object so this isn’t necessary… but I couldn’t find how!