get color of an object from sceene

Hi, I have a function that sends rays from camera. If the ray hits to an object function returns red and if not returns white. I want to get the color of a point if a ray hits an object and return it instead of red and return white if there is nothing there. How can i get the color?

function TracePixel (x : int, y : int) {
	
	var ray : Ray = camera.ScreenPointToRay(Vector3(x * resolution,y * resolution,0));
	

	if (Physics.Raycast(ray,rayDistance)) 
	{
		return Color.red;
	}
	else {
		return Color.white;
	}
}

var hit : RaycastHit; returns some information about the point but i could not reached the color of the point. Is it possible to get that color?

Hi, to be able to get the color of hit point, reach renderer of gameobject of hit and check below as:

Renderer x; // The renderer renderer which want to take the color of your gameObject.

void Update(){
    UpdateMouse();
}

void UpdateMouse(){
	ray = uICamera.ScreenPointToRay(Input.mousePosition);

   	if (Physics.Raycast(ray, out hit)){
	     if(hit.collider.name == "Name of gameobject in Inspector")
		      x.material.color = Color.red;
         else
              x.material.color = Color.white;
}

Sorry for my english :slight_smile: