How to run a trace from mouse to object and return the hit object

Ok, I am getting really frustrated and feeling really stupid right now. I am new to unity but not to scripting and I am running into what is probably one of those minor differences between engines that takes weeks to figure out. All I am trying to do is basically figure out what my mouse is over. BUT the MouseOver() function is only called on individual objects, so for me to detect what is under the mouse at any given time every single object in the game would need to be running a script and returning that information to the object that actually needs said information. OR I could run a trace from the mouse and have it return the gameobject it hits.

Here is what I have tried:

var HitObject;
var Visible = true;



function Start () 
{

}



function Update () 
{
	CheckForInteract();
}



function CheckForInteract()
{
	//if right click
	if(Input.GetAxis("Fire2"))
	{
		GetMouseHit();
	}
}


//Check what is under mouse
function GetMouseHit()
{
		//set ray start to the position of our in game cursor object
		var StartPos = transform.position;
		
		//move start y just a bit so we dont hit the cursor itself (I should relay figure out how to make it ignore the cursor entirely)
		StartPos.y = 2;
		
		//I dont get how the heck this gets filled
		var hitInfo : RaycastHit;
		
		//Run a trace from the cursor down and store the information in hitInfo?
		Physics.Raycast(StartPos, Vector3.down, hitInfo);
		
		//Try to draw a line to see why the heck this is doing the weird stuff it is doing
		//DOES NOT WORK NO GREEN LINE DRAWN AT ALL ANYWHRE!!!
		Debug.DrawLine(StartPos, Vector3.down * (200), Color.green, 2, false);
		
		//Record what has been hit for later use
		//IT DOES NOT WORK JUST THROWS UP ERRORS
		HitObject = hitInfo.collider;
		
		//Try to figure out what we are actualy hitting
		Debug.Log(hitInfo.collider);
		Debug.Log(HitObject);


}

This code runs on the object I use for my cursor which also runs a script which positions it at the mouses position. For whatever reason (that I cant understand because Debug.DrawLine does not work) It seems to be drawing a ray not from the cursor strait down, but from the cursor to the cameras center. Making it useless since instead of telling me what is under the mouse it tells me what I am looking at. Which is always the player since the player is always in the center of the camera!

Thank you for any help you can provide.

This is what you need.

Thanks for the help, I was able to figure it out from there, although a little better explanation would have been nice. Here’s the working code, it should run on any object now. I got rid of the cursor object since it kept getting in the way of the ray-cast.

#pragma strict

var HitObject;
var Visible = true;



function Start () 
{

}



function Update () 
{
	CheckForInteract();
}



function CheckForInteract()
{
	//if right click
	if(Input.GetAxis("Fire2"))
	{
		GetMouseHit();
	}
}


//Check what is under mouse
function GetMouseHit()
{
	// get mouse location 2D
	var mousePos = Input.mousePosition;
	
	//create ray from mouse and... yeah actualy I dont know how this works.
	var ray : Ray = camera.main.ScreenPointToRay (mousePos);
	
	//This still does not work
	Debug.DrawRay (ray.origin, ray.direction * 100, Color.green);
	
	//Used to store what we hit
	var hitInfo : RaycastHit;
	
	//use ray to find object
	Physics.Raycast(ray.origin, ray.direction, hitInfo);
	
	//tell us what we hit
	Debug.Log(hitInfo.collider);
}