- Home /
Help with multi menu closing using bool
Hey all, I'm in the process of learning unity c# and I'm having trouble with menus in a tower defense game that I am making.
The problem I have is that I have lots of build platforms that spawn towers and I'm trying to stop players from being able to click on another platform while the menu is already open by closing the current/all menus that are open.
I was able to do this while only using 1 platform but I ran into problems when I added more. Should I focus on arrays, events or is there something else I need to look into in order to get my script to close multi menus?
Also note, all platforms are prefabs that the same script so I have 1 script being ran by 20-40 platforms.
The following code is on an empty game object that covers the scene and prevents the player form clicking on anything but the empty game object when a menu is open
public class closemenu5 : MonoBehaviour
{
public GameObject myTower;
public GameObject clickBlocker;
clickmenutest script;
void OnMouseDown ()
{
myTower.GetComponent<clickmenutest>().showMenu = false;
clickBlocker.SetActive(!true);
}
}
If more code is needed please let me know.
So you don't want multiple menus? Why not have just one script for the menu, or am i missing something?
think of it like this.
I have 7 cubes, when you click on a cube it will open a menu so you can build on it, but if you click on another cube with out building something then another menu will open up.
so I added a empty game object that covers the scene once a menu is open but I am not able to get the click blocker to close menus with out hard coding each cube.
I am trying to find a way to make the clickblocker turn the show$$anonymous$$enu bool to false on every cube with a On$$anonymous$$ouseDown.
Please let me know if this is still unclear and I will try to explain it another way.
Is there a reason why you need to have menu script in every cube? You could have the menu script in the empty object and just get the needed data from the cube (when clicked).
Could you give example of your menu script, it would make it easier to understand?
The menu needs to be on each cube because the cube is acting as a placeholder on where to build the towers and I'm trying to make everything modular. the following is the menu script that I have attached to the cubes. This just seemed the right way to do the build menus and if I'm making it hard on my self please let me know.
using UnityEngine;
using System.Collections;
public class clickmenutest : $$anonymous$$onoBehaviour
{
public Rect windowPos = new Rect(10,10,200,150);
public GameObject cannon;
public GameObject clickBlocker;
public bool show$$anonymous$$enu = false;
// Set showing the menu to false
void On$$anonymous$$ouseDown()
{
show$$anonymous$$enu = !show$$anonymous$$enu;
clickBlocker.SetActive(true);
}
void OnGUI()
{
if(show$$anonymous$$enu)
windowPos = GUI.Window(0, windowPos, drawWindow, "Tech Tree");
}
void drawWindow(int aID)
{
if(show$$anonymous$$enu)
if(GUI.Button(new Rect(20,40,80,20), "Cannon"))
{
Instantiate(cannon, transform.position, transform.rotation);
show$$anonymous$$enu = false;
}
}
}
I still don't see need to have menu script in every cube if you just want 1 menu window.
$$anonymous$$ake empty gameobject named "$$anonymous$$enu" and add menu script to it
using UnityEngine;
using System.Collections;
public class menu_script : $$anonymous$$onoBehaviour
{
public Rect windowPos = new Rect(10,10,200,150);
public GameObject selected_object;
public GameObject cannon;
public bool show$$anonymous$$enu = false;
//etc...
and when you click a cube (this is for the cube script):
void On$$anonymous$$ouseDown()
{
GameObject menu;
menu = GameObject.Find("$$anonymous$$enu");
menu.Send$$anonymous$$essage("Open$$anonymous$$enu", gameobject); //sends message to menu_script
}
Then menu script receives the message:
public void Open$$anonymous$$enu(GameObject obj)
{
selected_object = obj;
show$$anonymous$$enu = true;
}
void OnGUI()
{
if(show$$anonymous$$enu)
windowPos = GUI.Window(0, windowPos, drawWindow, "Tech Tree");
}
void drawWindow(int aID)
{
if(show$$anonymous$$enu)
if(GUI.Button(new Rect(20,40,80,20), "Cannon"))
{
Instantiate(cannon, selected_object.transform.position, selected_object.transform.rotation);
show$$anonymous$$enu = false;
}
}
EDIT:
Please tell me if there is an error, because i haven't tested this.
Answer by Maldrasou · Oct 25, 2014 at 06:43 AM
I was finally able to get the system to work the way I wanted it to by using FindGameObjectsWithTag.
I added the tag "tow" to each platform and used the following code
`using UnityEngine;
using System.Collections;
public class closemenu5 : MonoBehaviour
{
public GameObject clickBlocker;
clickmenutest script;
void OnMouseDown ()
{
foreach(GameObject myTower in GameObject.FindGameObjectsWithTag("tow"))
{
if(myTower.name == "myTower")
{
myTower.GetComponent<clickmenutest>().showMenu = false;
clickBlocker.SetActive(!true);
}
}
}
}`