I am currently trying to get the ID of the gameobject that I click upon. All the gameobjects each have a 2D box collider that is set to being a trigger. But when I click on the gameobjects my 2DRaycast returns null. Not sure what I am doing wrong but any help is greatly appreciated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CombatScript : MonoBehaviour
{
public bool Player1Turn;
string cardScript;
// Start is called before the first frame update
void Start()
{
Player1Turn = true;
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Debug.Log("Mouse Clikced");
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);
RaycastHit2D hit = Physics2D.Raycast(mousePos2D.normalized * 100f, Vector2.zero);
Debug.Log(hit.collider);
if (hit.collider != null)
{
if (Player1Turn)
{
cardScript = hit.collider.gameObject.tag.ToString() + "Stats";
Debug.Log(cardScript);
}
}
}
}
}