x


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.

more ▼

asked Jun 01 '12 at 09:21 PM

lockedbob gravatar image

lockedbob
0 1 2

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.

Jun 01 '12 at 09:29 PM kolban

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

Jun 02 '12 at 02:08 PM Bunny83
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Jun 01 '12 at 09:47 PM

wilco64256 gravatar image

wilco64256
150 6 13 18

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x788
x215

asked: Jun 01 '12 at 09:21 PM

Seen: 333 times

Last Updated: Jun 05 '12 at 06:22 AM