Raycasting problems

Hey all,

I’m trying to figure out raycasting by following a tutorial and nothing is working. I’ve attached the script to my camera and the click runs just fine but the ray isn’t created. Also the game is in 2d if that matters at all.

Here is my code:

using UnityEngine;
using System.Collections;

public class click : MonoBehaviour {
	//public Camera camera;
	
	void Update(){
		if (Input.GetMouseButtonDown (0)) {
			RaycastHit hitInfo;
			Ray rayOrigin = Camera.main.ScreenPointToRay (Input.mousePosition);
		
			if (Physics.Raycast (rayOrigin, out hitInfo, 25f)) {
				Debug.Log ("yo");
			}
		}
	}
}

As your game is in 2D, I assume you are using the full 2D system Unity has and not just locking the camera to a 2D orthographic view. In that case, you need to use Physics2D for all of your collision and raycasting code. Here is the docs link to help you out a bit on syntax:
Physics2D.Raycast

You will also need to make sure you have colliders on your objects for the raycast to hit, otherwise it will pass through the object as it if wasn’t there.