Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 martin-rohwedder · Nov 07, 2015 at 01:39 PM · 2duigameobjectinputmouseclick

Open and close UI Menu on background mouse click

Hello all

I am trying to make a point and click game in unity 5.2.2.

My problem is that I need to have a UI menu to open, when the player clicks with the left mouse button on the background sprite, and it should close again, when he click on it again. I have created a script, and so far it works like that, but It also closes, when I click on the menu buttons itself, which means that I can't use the menu which opens. Please help me to how I can achive that.

The way the menu should work, is like in the game Game Dev Tycoon, where the player can open and close the menu, when clicking on the game background.

Here is my script, in its current form.

 using UnityEngine;
 using System.Collections;
 /// <summary>
 /// This is the main class, for the entire game. The purpose of this class, is to control any gameplay in the game.
 /// </summary>
 public class GameManagerScript : MonoBehaviour {
 
     public GameObject gameUI;                                           //The actual Game UI
 
     public void Awake()
     {
         gameUI.SetActive(false);
     }
 
     // Game Manager Game Loop
     public void Update()
     {
         CheckForKeyInput();
     }
 
     void CheckForKeyInput()
     {
         //Mouse0 = First mouse button
         if (Input.GetKeyDown(KeyCode.Mouse0))
         {
             Debug.Log("Mouse0 button fired!");
             gameUI.SetActive(!gameUI.activeSelf);
         }
         //If escape, exit the game
         else if (Input.GetKeyDown(KeyCode.Escape))
         {
             Debug.Log("Application has been exited");
             Application.Quit();
         }
     }
 
 }


Comment
Add comment
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

1 Reply

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

Answer by DeathDev · Nov 07, 2015 at 07:29 PM

You seem to be checking for mouse input, not mouse input on that specific object.
If you add a box collider2D to that object(or if it already has one), you should be able to use the OnMouseDown function in a script that is a component of your background sprite.
Here's an example:

 using UnityEngine;
 using System.Collections;
 public class BackgroundManager : MonoBehaviour
 {
     GameObject gameManager; //The object that holds the gameManager script.
     bool UIActive = false;
     void OnMouseDown()
     {
         gameManager.GetComponent<GameManagerScript>().gameUI.SetActive(false);
     }
 }

If you intend to change a variable in your GameManagerScript class, you could do one of the following:
A) Get a reference to the gameobject that holds the GameManagerScript class as a component, then get a reference to that component.(I have the example script set up for this. :p)
B) Change your GameManagerScript class to a static class, meaning that all other scripts can access it.(You should note if you do this, that there would no longer be multiple instances of the script.)

Comment
Add comment · Show 1 · 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
avatar image martin-rohwedder · Nov 07, 2015 at 08:04 PM 0
Share

Thanks.. I got it to work :)

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

53 People are following this question.

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

Related Questions

is possible press 2 buttons at the same time Mobile touch? 0 Answers

Unity UI : Attached Gameobjetcs resets when starting the game 1 Answer

is it Possible to use navigation on non UI elements? 1 Answer

Touch a specific area on the screen 2 Answers

Detect UI Panel click 2 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