1
\$\begingroup\$

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();
        }
    }
}
\$\endgroup\$
1
\$\begingroup\$

You're probably looking for;

if(!EventSystem.current.IsPointerOverGameObject())
    ...

This method returns true when the mouse pointer is over a UI component. You're menu is simply being toggled when Mouse0 is pressed. Include this inside your if statement to stop the menu being toggled when the user is over a UI element.

Hope this helped.

\$\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.