0
\$\begingroup\$

I am trying to make a menu system for each GameObject, and when a GameObject is clicked it will close all other menus and open the one for that GameObject. I am using one script for this:

using UnityEngine;
using System.Collections;

public class BlockMenus : MonoBehaviour 
{   
Vector3 point;

public Transform block;
public GameObject activeBlock;

bool buttonActive = false;
bool activeBlockActive = false;

string hitObject = "";

void Update()
{
    point = Camera.main.WorldToScreenPoint(block.position);
}

void OnMouseDown()
{
    hitObject = this.gameObject.transform.tag;

    buttonActive = !buttonActive;
    activeBlockActive = !activeBlockActive;

    activeBlock.SetActive(activeBlockActive);
}

void OnGUI()
{
    if(buttonActive)
    {
        switch(hitObject)
        {
            case "mine":
                GUI.Box(new Rect(((point.x) * (1.08f)), ((Screen.height - point.y) * (0.1f)), (Screen.width * (0.3f)), (Screen.height * (0.5f))), "");
                GUI.Button(new Rect(((point.x) * (1.08f)), ((Screen.height - point.y) * (0.87f)), (Screen.width * (0.05f)), (Screen.height * (0.06f))), "Build Mine");
                break;
        }
    }   
}
}
\$\endgroup\$
  • \$\begingroup\$ What is your goal? What is your exact question/problem? \$\endgroup\$ – n_plum Feb 2 '17 at 17:47
  • \$\begingroup\$ I want to make the script close every other GUI except for the one that was just opened. But I cannot figure out how to do so. \$\endgroup\$ – Tyler Feb 2 '17 at 17:50
  • 1
    \$\begingroup\$ What do you mean by every other GUI? The inspector gui or some third party one? Do you have an example? \$\endgroup\$ – user3797758 Feb 2 '17 at 17:57
  • \$\begingroup\$ By GUI I mean everything in the OnGUI() method \$\endgroup\$ – Tyler Feb 2 '17 at 18:05
  • 1
    \$\begingroup\$ Why are you using the old gui system if this is unity5? \$\endgroup\$ – johnny 5 Feb 2 '17 at 21:37
0
\$\begingroup\$

This may work/help. The best solution I can muster/find is toggling the GUI objects individually and not just setting them all at once. You don't need to do it in a switch really. This is a mix of your code and the solution I found.

using UnityEngine;
using System.Collections;

public class BlockMenus : MonoBehaviour 
{   
    Vector3 point;

    public Transform block;
    public GameObject activeBlock;

    bool buttonActive = true; 
    bool activeBlock = false; 

void Update()
{
    point = Camera.main.WorldToScreenPoint(block.position);
}

void OnMouseDown()
{
    buttonActive = !buttonActive; /* Turn button off */
    activeBlock = !activeBlock; /* Turn block on */
}

void OnGUI()
{
    if(buttonActive) /* Button is live */
    {
       GUI.Button("Button"); /* Put button specs in */
    }   
    if(activeBlock){ /* Block is live */
       GUI.Box("Box"); /* Put box specs in */
    }
}
}

Also I realized the need to use the !. Makes sense if you are only using the mouseDown. Alternatively you could add an opposite mouseUp to swap things back, but both should do relatively the same thing. Although if you use the mouseUp you may end up with a very fast switch every time you click.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.