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
0
Question by lockedbob · Jun 01, 2012 at 09:21 PM · buttondisable

Disable buttons for individual game objects.

Hello, I ran into a problem that I can't seem to fix. I have two scripts: One that displays a group of 3 buttons when I click a floor tile and another that displays 3 buttons when I click an object built on that tile.

The problem is that at the moment if I click multiple tiles/buildings in a row the buttons from the previous selected object stay active. Is there any way (apart from using object Tags) to prevent this from happening?

Here is the class that handles the button creation. As you can see from the code I temporarily fixed the issue by creating an access property for the boolean _isPressed and each time I try to display buttons in another location I grab an array of all other objects of that type and disable them.

I was wondering if there is any other way in which I can do this. Something perhaps more optimal or usable without tags? I fail to see a solution to this issue but perhaps now that you see the code you can help me a bit more.

public class BuildingMenu : Stats { private Vector3 _mousePos; private bool _buttonsShown = false; private GameObject[] _buildings; private bool _isPressed = false;

 private float X_POS;
 private float Y_POS;
 private float WIDTH;
 private float HEIGHT;
 
 void Awake()
 {
     X_POS = 0;
     Y_POS = Screen.height - Screen.height / 3;
     WIDTH = Screen.width;
     HEIGHT = Screen.height / 3;
 }    
 
 void OnMouseUpAsButton()
 {
     //building was clicked
     IsPressed = true;
     
     Debug.Log(GetComponent<Stats>().Health);
     
     //check if other buildings have buttons displayed and turn them off
     FlipButton();

     //freeze mouse position if buttons were already shown for this tile. This avoids a case where buttons move around each time they are clicked
     if(IsPressed && !_buttonsShown)
     {
         _mousePos = Input.mousePosition;
         _buttonsShown = true;
     }
 }
 
 public void FlipButton()
 {
     _buildings = GameObject.FindGameObjectsWithTag("Wall");
     
     foreach(GameObject building in _buildings)
     {
         if(building.GetComponent<BuildingMenu>().IsPressed && IsPressed)
         {
             building.GetComponent<BuildingMenu>().IsPressed = false;
             IsPressed = true;
         }
     }
 }
 
 void OnMouseExit()
 {
     _buttonsShown = false;
 }

 void OnGUI()
 {
     drawButtons();
 }
 
 private void drawButtons()
 {
     if(IsPressed)
     {
         if(GUI.Button(new Rect(Screen.width - (Screen.width - _mousePos.x) - 3 * NameConsts.BUTTON_OFFSET, Screen.height - _mousePos.y, NameConsts.BUTTON_WIDTH, NameConsts.BUTTON_HEIGHT), "Sell\n" + Cost/2 + "gold"))
         {
             Destroy(gameObject);
             IsPressed = false;
         }
         
         if(GUI.Button(new Rect(Screen.width - (Screen.width - _mousePos.x) - NameConsts.BUTTON_OFFSET, Screen.height - _mousePos.y - 3 * NameConsts.BUTTON_OFFSET, NameConsts.BUTTON_WIDTH, NameConsts.BUTTON_HEIGHT), "Upgrade"))
         {
             IsPressed = false;
         }
         
         if(GUI.Button(new Rect(Screen.width - (Screen.width - _mousePos.x) + NameConsts.BUTTON_OFFSET, Screen.height - _mousePos.y, NameConsts.BUTTON_WIDTH, NameConsts.BUTTON_HEIGHT), "Destroy"))
         {
             IsPressed = false;
         }    
     }
 }

 public bool IsPressed
 {
     get
     {
         return _isPressed;
     }
     set
     {
         _isPressed = value;
     }
 }
 },

Thank you for taking the time to read this.

Comment
Add comment · Show 2
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 kolban · Jun 01, 2012 at 09:29 PM 0
Share

Hi, your description of tiles and buttons gives us a feel of the nature of the game but not the technical nature of the issue with which we may assist. Generically, most software builds "state" and it is your logic that controls the transitions from one state to another. Without seeing a well explained description of the nature of your code, there is probably going to be little we can do to assist.

avatar image Bunny83 · Jun 02, 2012 at 02:08 PM 0
Share

@lockedbob:
I guess your answer should be part of your question, right? I've converted it to a comment, but you might want to edit your question and add the information there. If you do so, please remove the comment. If you can't remove it (due to low karma), just add a comment and one of the high-karma people will delete it for you.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by wilco64256 · Jun 01, 2012 at 09:47 PM

You need to have something in your code that's spawning the buttons that checks to see if you've already displayed them somewhere else and then removes those before showing the new ones.

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

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Disable Button for a while 2 Answers

Double click too fast 0 Answers

How can i disable left button mouse, when i click on GameObject? 2 Answers

Disable/Enable Buttons and Text plz help jc 1 Answer

Disable Sprite when keypress (Working Script, just need disble) 0 Answers


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