I am currently trying to make a small program via Unity and I would need a kind of Clear button that resets all toggles to off. I thought about just writing a script that would one by one reset it but it would take a lot of time to do I would have to change a lot of things to static which is more time.
The program is basically a shopping cart, you choose an item(toggle), it adds the value to text so it can be seen and sums up every other toggle (36 products). After you are done you can see the price, but to continue with another one you would have to switch every toggle back down.
TLDR: How to reset toggles in Unity?
CodePudding user response:
You could add the script to the outermost container and then use GetComponentsInChildren<Toggle>()
to get all the toggles in your form. Then you can just iterate over that array.
If the amount of toggles is fixed, you could cache that array in Start()
. If it's dynamic, you could also assign the newly created toggles to a central list somewhere to access them.