Issue with 2D raycast
I keep getting the error "NullReferenceException: Object reference not set to an instance of an object NewBehaviourScript.CastRay () (at Assets/Scripts/NewBehaviourScript.cs:22) NewBehaviourScript.Update () (at Assets/Scripts/NewBehaviourScript.cs:15)"
I keep going through Raycast and Raycast2D information throughout the forums and I cannot figure out what I have done wrong. I did make sure that the objects in question also have the appropriate 2D Colliders.
First post so apologize for mistakes..
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour {
void Start(){
}
void Update() {
if (Input.GetMouseButtonDown(0)) {
Debug.Log("Pressed left click, casting ray.");
CastRay();
}
}
void CastRay() {
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
Debug.Log ("Finally working");
}
Your answer

Follow this Question
Related Questions
Raycast is hitting enemy even when not in contact with it? 2 Answers
CircleCast hit its origin... 0 Answers
Raycast 2d don't work 0 Answers
NullReferenceException while raycasting? 0 Answers
Ray null reference exception and wrong direction when casting it 0 Answers