Select Gameobjects only with certain tag.

Hey guys,

I am trying to be able to click on objects with a certain gametag. In this case it’s the tag “POS”. So I have a plane and several objects I want to be able to select. Or at least show the name in the console. The plane is untagged and the cubes have the POS tag.

Why am I able to click on the plane? Even though it doesn’t have the tag “POS”?

Thank you for your help!
Best regards!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class POS_Item : MonoBehaviour
{

    // Update is called once per frame
    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            if (Physics.Raycast(ray, out hit, 100.0f))
            {
                // if (hit.transform != null && FindObjectsOfType.tag == "POS")
                if (gameObject.tag == "POS")
                {
                    PrintName(hit.transform.gameObject);
                }
            }
        }
    }

    private void PrintName(GameObject go)
    {
        print(go.name);
    }
}

Hello.

Here:

           if (Physics.Raycast(ray, out hit, 100.0f))
            {
                // if (hit.transform != null && FindObjectsOfType.tag == "POS")
                if (gameObject.tag == "POS")
                {
                    PrintName(hit.transform.gameObject);
                }
             }

why check for the gameobject.tag?

You should check for hit.collider.gameobject.tag

Bye!