I want to create something simple, where when you click an object, they will destroy themselves.
I’m sending out a ray, and then checking if the object hit has a tag called “Nails”
private void Update()
{
if (SwitchCam._deskMode==true)
{
RemoveSator.satorCoverInstance.transform.position = SwitchCam.objectSpawn.transform.position;
RemoveSator.satorCoverInstance.transform.rotation = Quaternion.Euler(0, 0, -90);
}
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
Debug.Log("Sent out ray");
CapsuleCollider cc = hit.collider as CapsuleCollider;
if (cc!=null && cc.gameObject.CompareTag("Nails"))
{
Debug.Log("Clicked");
Destroy(cc.gameObject);
}
}
}
}
Up until line 16, everything works. Using if(cc!=null) as a condition itself works,and all objects clicked who have a capsule collider are destroyed. But adding the part that verifies if the object has the tag “Nails” does not.
The tag is assigned on my gameobjects in the inspector, and all of the objects have capsule colliders, so I’m not sure what’s causing the issue.
If the object could be destoryed with cc.gameObject, I don’t know why CompareTag doesn’t work when I refer to cc.gameObject.
