Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Bob5602 · Jan 10, 2011 at 03:38 PM · guibuttoncancel

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bob5602 · Jan 10, 2011 at 06:44 PM 0
Share

Hm that makes sense. I don't know how this Guistates system works, but, I figure I can figure it out.

avatar image
0

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bob5602 · Jan 10, 2011 at 06:43 PM 0
Share

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.

avatar image Noise crime · Jan 11, 2011 at 01:04 AM 0
Share

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.

avatar image Noise crime · Jan 11, 2011 at 01:06 AM 0
Share

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'.

avatar image Bob5602 · Jan 11, 2011 at 02:08 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges