Check for selected toggles and display appropriate GameObjects
Hello everybody,
I have two ScrollRects and each one contains multiple products that the user can select using toggles. Once the user has made his/her selection he/she can click on a Start button to progress. Based on his selection I want to show the corresponding selected products in the scene.
Therefore, I would like to find out which toggles are on (which items are selected) and activate the appropriate GameObjects (e.g. using SetActive(true)) upon button click.
ToggleGroup will not work for me since the user can select multiple items. I tried creating a List with all the products, going through the toggles to check their isOn, add selected products in a new a list, and link them to GameObjects...all that seems too complicated and got me nowhere.
What would you propose?
Thanks, Michael
Answer by michaeldoukas · Nov 20, 2017 at 09:56 AM
In order to find the selected toggles I am doing something like this:
private List<GameObject> selectedProducts = new List<GameObject>();
public void SelectedProducts()
{
GameObject[] products = GameObject.FindGameObjectsWithTag("products");
foreach (GameObject product in products)
{
if (product.GetComponent<Toggle>().isOn)
{
selectedProducts.Add(product);
}
}
}
I am stuck now at the next part where I have to make the connection between the toggles and the actual game objects. As I said, these toggles correspond to inactive GameObjects that have to be activated based on user selection.
Any ideas?