- Home /
 
 
               Question by 
               jakeash11221212 · Nov 11, 2019 at 03:01 PM · 
                texture2dmaterials  
              
 
              Texture won't apply to plane at runtime. (HDRP)
I wrote a script to dynamically generate a sheet of cells as a texture. They do successfully generate (I've saved and opened them), and the script does run to completion with no errors. However, no matter what I do, the texture does not appear on the material. So far I've tried, enabling KeyWords and switching shaders. I am using HDRP/Unlit, and this is my code:
 public class TexturedPane : MonoBehaviour {
     public int width = 1;
     public int height = 1;
     public bool border = true;
     public Color color;
 
     private const int tileSide = 10;
     private const int borderWidth = 1;
     private const int innerWidth = 4;
 
     private void Update() {
         if (Input.GetKeyDown(KeyCode.Space)) {
             Texture2D tex = new Texture2D(10 * width, 10 * height, TextureFormat.ARGB32, false);
             for (int r = 0; r < height * tileSide; r++) { // CLear the texture and fill the border
                 for (int c = 0; c < width * tileSide; c++) {
                     if (border && (r == 0 || c == 0 || r == height * tileSide - 1 || c == width * tileSide - 1))
                         tex.SetPixel(c, r, color);
                     else
                         tex.SetPixel(c, r, Color.clear);
                 }
             }
             int offset = (tileSide - innerWidth) / 2;
             Color[] colors = new Color[innerWidth * innerWidth]; // This is to satisfy the array requirement of SetPixels, I am aware this is an awful way to do it
             for (int i = 0; i < colors.Length; i++)
                 colors[i] = color;
             for (int r = 0; r < height; r++) {
                 for (int c = 0; c < width; c++) {
                     tex.SetPixels(c * tileSide + offset, r * tileSide + offset, innerWidth, innerWidth, colors);
                 }
             }
             // This is where the problem is, all the above code works fine
             // but nothing happens with the material.
             tex.Apply();
             Renderer renderer = GetComponent<Renderer>();
             renderer.material.SetTexture("_MainTex", tex);
         }
     }
 }
 
              
               Comment
              
 
               
              Your answer
 
             Follow this Question
Related Questions
Texturing a large surface 2 Answers
Problem with textures ? 1 Answer
Best Practice to Combine many textures 0 Answers
Material not updating on Image 2 Answers
Get Texture 2D after all shader passes are applied 0 Answers