title: Using Raycasts.TextureCoord to draw onto 3d objects - not drawing
body: <p>I am currently developing a game where you can use the mouse to draw on 3d objects, however when I use the mouse to draw it does not mark the texture. I am using Unity Personal Edition. I followed the <a href="https://docs.unity3d.com/ScriptReference/RaycastHit-textureCoord.html" rel="nofollow noreferrer">Raycast.TextureCoord</a> documentation which has an example. I used the following C# code, almost exactly the same as the example:</p>
  
  <pre><code>using UnityEngine;
  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class Paint : MonoBehaviour
  {
      public Camera cam;
  
      void Start()
      {
          cam = GetComponent&lt;Camera&gt;();
      }
  
      void Update()
      {
          if (!Input.GetMouseButton(0))
              return;
  
          RaycastHit hit;
          if (!Physics.Raycast(cam.ScreenPointToRay(Input.mousePosition), out hit))
              return;
  
          Renderer rend = hit.transform.GetComponent&lt;Renderer&gt;();
          MeshCollider meshCollider = hit.collider as MeshCollider;
  
          if (rend == null || rend.sharedMaterial == null || rend.sharedMaterial.mainTexture == null || meshCollider == null)
              return;
  
          Texture2D tex = rend.material.mainTexture as Texture2D;
          Vector2 pixelUV = hit.textureCoord;
          pixelUV.x *= tex.width;
          pixelUV.y *= tex.height;
  
          tex.SetPixel((int)pixelUV.x, (int)pixelUV.y, Color.black);
          tex.Apply();
      }
  }
  </code></pre>
  
  <p>I have this code in a script called paint. I created an empty project which has a texture with the read and write mode on with the convex off. The camera faces a cube and a plane. There is no moving functionality whatsoever. There is just a camera, attached is the paint c# script with the main camera attached. I have followed the instructions however it does not paint on to the cube when I click on to it. Why is this? How can I make it paint on to the cube.</p>
  
  <p>Apologies if this is obvious as I am new to unity.</p>
  
  <p><a href="https://i.stack.imgur.com/UBA3l.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UBA3l.png" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/fjJ9l.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fjJ9l.png" alt="enter image description here"></a></p>