• 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 Maldrasou · Jul 27, 2014 at 07:25 AM · arraygetcomponenteventsbooldelegate

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.

Comment
Add comment · Show 7
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 Hullu · Jul 27, 2014 at 07:42 AM 0
Share

So you don't want multiple menus? Why not have just one script for the menu, or am i missing something?

avatar image Maldrasou · Jul 27, 2014 at 07:56 AM 0
Share

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.

avatar image Hullu · Jul 27, 2014 at 08:14 AM 0
Share

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?

avatar image Maldrasou · Jul 27, 2014 at 09:11 AM 0
Share

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;
     
                     }
     
                 }
     }

avatar image Hullu · Jul 27, 2014 at 09:56 AM 0
Share

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.

avatar image Maldrasou · Jul 28, 2014 at 05:11 AM 0
Share

I've never used Send$$anonymous$$essage before, I'm reading up on it at this time to make sure I'm doing it right.

avatar image Maldrasou · Jul 29, 2014 at 01:14 AM 0
Share

I am receiving a Send$$anonymous$$essage Open$$anonymous$$enu has no receiver error that points to

 menu.Send$$anonymous$$essage("Open$$anonymous$$enu", gameobject);

I'm new to the send message function so the mistake is most likely on my end and something simple.

1 Reply

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

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);
             }
     
         }
 
       }
 }`


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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to make an Array equal the Components of an Instantiated GameObject 1 Answer

How do I change a clicked on GameObjects variable when a boolean (Next Turn) is true? 1 Answer

Dynamic Anonymous Delegates/Lambda On UntyEvent 1 Answer

Game manager Object reference not set to an instance of an object 1 Answer

GetComponent with arrays C#? 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