- Home /
Toolbar question...
Hello. How can I make a toolbar where each option corresponds to a game object in the scene (and the game objects are only active when the corresponding button is selected)?
Answer by Jesse Anders · Feb 01, 2011 at 02:07 AM
Use the GUI 'selection grid' control, and keep track of its current value (an integer). Create an array of game objects with as many elements as there are entries in the selection grid, and populate it using the method of your choice (e.g. assigning from inspector, querying by name or tag, etc.).
Then, at the end of your OnGUI() function, iterate over the game object array using an indexed 'for' loop, and for each game object, make it active if the index is the same as the current value of the selection grid, and make it inactive otherwise. (This can of course be optimized by only activating/deactivating objects when the selection grid value changes, and/or only deactivating the last game object that was active, rather than deactivating all of them every time.)
Your answer
![](https://koobas.hobune.stream/wayback/20220613102132im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Paper, Rock, Scissor 3 Answers
Add a button to toolbar/selection grid? 1 Answer
Attaching Scripts to GUI Components 1 Answer
How make a toolbar equals a RTS games? 1 Answer
OnMouse* functions for Toolbar elements? 0 Answers