- Home /
Get rid of GUIButtons more efficiently
Hey everyone.
I am building my space tower defense game (cliched!) and am running into a funny issue :) Allow me to explain the situation. I have multiple boxes that I am using as turret spawn points, and am using the built in onGUI button ability in Unity to create buttons. My current workflow :
If you click on a button, it gets checked as "clicked" (boolean), meanwhile the onGUI checks to see if "clicked" is true. If it is true, it takes the last position that the mouse was (on the box) and create a GUI button on top of it. Everything works great! I added some code to make it so I can change clicked to false when I right click, or press escape, so that someone can click on a spawnbox, get the turret selection gui button, and if they choose not to click on a button they can right click off to the side somewhere and cancel out of the selection.
This all works great! However, I am looking for a way to turn off the buttons on a given spawnbox when ANOTHER spawnbox is pressed. At the moment, if I click on multiple spawnboxes, it makes multiple instances of the selection buttons until I click on an option or I right click cancel.
My current way of dealing with this is a function inside each spawnbox that I call each time a spawnbox is clicked. It creates an array of all the spawnboxes, and for each one sets "clicked" to false, and it is called before the "clicked" property is set to true for the one box I have clicked.
This works, but creating an array with each click just to change one property seems kind of a waste of resources. Is there a more efficient way to get rid of these buttons if I click on another spawnbox?
Answer by The_r0nin · Jan 10, 2011 at 05:00 PM
A boolean global variable... something like "spawnBoxIsClicked". Define it in an unique object (something like "spawnRegulator"), and then each time a spawnbox is clicked, set it to true. When the click goes elsewhere (not on a box) or the GUI is used, set it back to false. Something like:
spawnRegulator.spawnBoxIsClicked = false;
should do the trick. This way you can either clear all boxes when you have a second click as true, or limit the player to clicking empty space before they can click the second box.
Answer by Loius · Jan 10, 2011 at 06:00 PM
I'd suggest keeping track of the GUI's overall state and only displaying relevant buttons -
enum GUIStates { TurretSelection, TurretPlacement, Pause // etc. }
var state : GUIStates = GUIStates.TurretSelection;
function OnGUI() { if ( clickedOnNothing ) state = GUIStates.TurretSelection; switch ( state ) { case GUIStates.TurretSelection: // show turret choices break; case GUIStates.TurretPlacement: // etc.
Hm that makes sense. I don't know how this Guistates system works, but, I figure I can figure it out.
Answer by Noise crime · Jan 10, 2011 at 06:35 PM
Personally i'd probably set up a separate single gui object (manager) and have the boxes call/broadcast to it when clicked. That way you have a single gui object/script dealing with your 'popup menu' display, making it far easier to manage the logic.
I had considered that, but was worried that setting the "clicked" on all spawnboxes would create a number of buttons at the same time, one for each spawnbox.
Nope thats the whole point of using a single script/object to manage the state, in those famous words 'there can be only one' ;) The spawnbox itself would no longer have a 'clicked' property ins$$anonymous$$d it would call a function in the single gui object and it would deter$$anonymous$$e what action to take, such as removing the button menu or disable/enable the button menu.
However really you should go with what you are most comfortable with, its not like one solution is right, though I feel my suggestion is more 'clean'.
You are most likely right. Currently, I went with r0nin's answer and put in a static var that checks for clicked that disables all of my other spawnboxes. You're basically proposing the same thing, except no individual clicks on the boxes. In the end I think yours would be more efficient.
Your answer
![](https://koobas.hobune.stream/wayback/20220613094812im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
GuiTexture (Touch button) unfollow camera! 1 Answer
Move GUI elements. 0 Answers
Can I use a movie texture with alpha as a GUI object? 1 Answer
Using GUI and check what button was pressed 1 Answer
GUI Button for Android 1 Answer