GetPixel in c#


need some help on code below

I have converted the below code from javascript to C#

RaycastHit hit = new RaycastHit();
        Ray ray = new Ray();
        ray = Camera.main.ScreenPointToRay(Input.mousePosition);        

        if (Input.GetMouseButtonDown (0)) 
            trace("mouse down");
            if (Physics.Raycast (ray, out hit)) 
               	Vector3 hitPos = Camera.main.WorldToScreenPoint(hit.point);
         		trace(hitPos+" - ";
         		Texture2D tex = hit.collider.renderer.material.mainTexture; 

		        Vector2 pixelUV = hit.textureCoord;
		        float uvX = pixelUV.x * tex.width;
		        float uvY  = pixelUV.y * tex.height; 
		        // If the hit point isn't transparent...
		        Color hitColor = tex.GetPixel(uvX, uvY);               

It is giving me Error on the lineTexture2D tex = hit.collider.renderer.material.mainTexture;

Need help urgently


Try this

Texture2D tex = hit.collider.GetComponent<MeshRenderer>().material.mainTexture;