Selecting objects with the mouse (event system)

Hello everyone.
I want to implement the ability to select certain objects on the screen. So far I have a script that selects any object at all.

I wanted to check the selectable object by belonging to SerializeField, but did not figure out how.
How can this check be implemented or done otherwise?

using UnityEngine;
using UnityEngine.EventSystems;

public class SelectSphere : MonoBehaviour
{
    [SerializeField] private Collider[] SelectList;
    EventSystem SelectEventSystem;

    void OnEnable()
    {
        SelectEventSystem = EventSystem.current;
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            if (UnityEngine.Physics.Raycast(ray, out hit))
            {
                if (hit.collider != null)
                {
                    GameObject SelectedGameObject = hit.collider.gameObject;
                    SelectEventSystem.SetSelectedGameObject(SelectedGameObject);

                    Debug.Log("Selected : " + SelectEventSystem.currentSelectedGameObject);
                }
            }
        }
    }
}

If by the variable SelectList you mean ListOfSelectableColliders, then when you get a click you need to get the collider involved, then then loop through the ListOfCollectiableColliders to see if it is in there, and decide based on its membership in that list what you want to do, perhaps add it to another list of selected items.

1 Like

Or you can use layers

using UnityEngine;

namespace ProjectDot
{
    public class UnityForum : MonoBehaviour
    {

        void Update()
        {

            MapObjectSelect();
            void MapObjectSelect()
            {

                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit hitHover;

                if (Physics.Raycast(ray, out hitHover))
                {


                    if (hitHover.transform.gameObject.layer == 10)
                    {
                        if (Input.GetMouseButton(0) && !mUI.popUpPanelVisible)
                        {
                            SelectObject(MapObjects.Wormhole.ToString(), galaxyObject.galaxyNumber);
                            mapCamera.SetCameraFocus(hitHover.transform);
                        }
                    }
                }
            }

        }
    }
}