How to assign a game object with a raycast?

Lets say I do a raycast like this:

    var mouseray = Camera.main.ScreenPointToRay(Input.mousePosition);
	var rayhitlocation : RaycastHit;
	layerMask = ~(1 << 9);//invert bitshift to exclude only layer 9 from raycast

	Physics.Raycast(mouseray, rayhitlocation, Mathf.Infinity, layerMask);

How would I assign a GameObject in my scene from whatever the raycast hit? So far, I think it’s purely syntax that I am getting wrong, but I’m not sure. I’ve also tried using the “collider.Raycast()” function like this:

  if (collider.Raycast(mray, rayhitlocation, Mathf.Infinity)) {
selected = true;//set rayhit object to selected
Unitmanager.unit = this.gameObject;
renderer.material.color = Color(.13,.97,.20,1);
//rigidhit = target.collider.attachedRigidbody;


I had another script with a static GameObject variable (script called Unitmanager), that I tried to assign the collider from the raycast to be the gameObject that it hit, but again it didn’t work…

So how can I grab a specific GameObject from a raycast? Should I use the collider.Raycast or the physics.Raycast as well?

Your almost correct, except that you’re doing all the modification on the gameobject casting the ray, which I doubt is what you want. You want the object hit by the ray to be modified, and you can access it with rayhitlocation.collider.gameObject.