- Home /
 
 
               Question by 
               santoshbhagat · Aug 24, 2012 at 01:37 PM · 
                texture2dgetpixel  
              
 
              GetPixel in c#
hi
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+" - "+hit.collider.name);
                  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
Thanks
               Comment
              
 
               
              Answer by Sundar · Aug 24, 2012 at 03:33 PM
Try this
 Texture2D tex = hit.collider.GetComponent<MeshRenderer>().material.mainTexture;
 
              Your answer