Detect if Cursor points to an Object?


I am currently working on my game and I want the player to be able to pick up objects. I thought about something which would not put a big text on the screen for the player, so I wanted to implement a system which will change the cursor to a hand or something and then make the player be able to pick up the certain object. I don’t know if I just couldn’t find it, but I haven’t seen this question on here. Either way, how can I get my script to detect if the cursor is pointing at the object? Any help would be appreciated, thanks in advance.

if you want the object itself to react to the mouse then the OnMouse Family of methods would be your best option,

but having each item being able react to the mouse individually might not be the greatest of ideas. if you wanted to just have the item give some kind of callback to the player then have the player determine how close that item is to them it might be more effective.

as an implementation suggestion maybe have the items track a OnMouseDown() then have it tell the player where that is, and then do your logic from there.

as an aside if you want the player to determine if there is an object “near” them (within radius-X) then I would simply suggest a sphereCast you might need to toy with radius a bit, but it should be effective.

I used a Raycast with a RaycastHit so I can get the GameObject from the RaycastHit. Of course you have to be careful with implementing the collider in the object. If you included it inside a child object of your object, the GameObject you will receive the child. Here is the code:

void Update () {

	if( Input.GetButtonDown("Fire1") ){
		Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		RaycastHit hit;
		if( Physics.Raycast(ray, out hit) ){
			GameObject selectedObject = hit.collider.gameObject;

			// get parent after selecting wall element
			if("Side") ){
				selectedObject = selectedObject.transform.parent.gameObject;
			_textSelection.text =;