0

I am a beginner in Unity and I am currently making a simple game. I have a problem where I need to select an object automatically by script so I can click outside of it without clicking on it in the screen.

What I want to do is when I click the object another object or panel will show and it will be automatically be selected by script so I can click outside the panel and it will close or will setActive to false. I tried the Select() and it is not working for me. The two objects are originally an Image but I added a Button component so it can be selectable.

This is the script for the object that will show the Panel:

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class ShowPhone : MonoBehaviour, ISelectHandler
{
    [SerializeField] Button phonePanel;

    public void OnSelect(BaseEventData eventData)
    {
        phonePanel.gameObject.SetActive(true);

    }
}

This is the script for the panel where I want to click outside to close it:

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class HidePhoen : MonoBehaviour, IDeselectHandler
{
    public Button phone;

    private void OnEnable()
    {
        phone.Select();
    }

    public void OnDeselect(BaseEventData data)
    {
        Debug.Log("The Object will be setActive false when clicked outside the object");
    }
}

1 Answer 1

1

You would use EventSystem.SetSelectedGameObject

EventSystem.current.SetSelectedGameObject(yourTargetGameObject);

Note though: Your panel would automatically be closed even if clicking any UI element within it (like e.g. InputField etc)


You can checkout the TMP_Dropdown source code and the way they implemented a "UI Blocker" which basically is an overlay over the entire screen except the drop-down popup and closes the pop-up when clicked on.

you could either replicate this or have a simpler version with a fully transparent background button behind your panel with onClick for closing the panel.

As alternative you could check RectTransformUtility.RectangleContainsScreenPoint

Sign up to request clarification or add additional context in comments.

5 Comments

The EventSystem.SetSelectedGameObject seems to be working, but it returns an error Attempting to select PhonePanel (UnityEngine.GameObject)while already selecting an object. How can I solve this error?
What I did is I replaced the Select() to SelectedGameObject in the OnEnable method. And it returns that error. Is there something wrong with the placement of the code?
When I put null to the SetSelectedGameObject so it can reset what is being selected, it returns the error Attempting to select while already selecting an object.. I checked the Scripting, but it should send OnDeselect on the previous selected object but it is not working.
I just gone with the fully transparent background button. I can't make the SelectedGameObject work.
I guess this happens because the OnEnable already happens as reaction to a button click and therefore a Select of that button in the same frame .. tbh no sure though how to overcome this except for delaying the select by a frame

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.