I am creating a function that inverts black/white pictures pixels from either black to white, or white to black. In a slightly different piece of working code, I can convert white pixels to a color generated by perlin noise. In this instance however, I am getting an error that the dimension of newTex is 0x0. I thought however, that I had created the texture with a width and height value. The line producing an error is line 27. mapPixels is declared on line 7, while newTex is defined on line 9. The error report reads - Texture '' is degenerate (dimensions 0x0) UnityEngine.Texture2D:SetPixels(Color[]) Spoiler: InvertPixelColors() Code (CSharp): public void InvertPixelColors() { int wid = pf.totalPixelsSquared; int hei = wid; Texture2D mapTex = (Texture2D)pixelMask.GetComponent<Renderer>().material.mainTexture; Texture2D newTex = new Texture2D(wid, hei); Color[] mapPixels = new Color[wid * hei]; for (int h = 0; h < hei; h++) { for (int w = 0; w < wid; w++) { Color pixColor = mapTex.GetPixel(w, h); if (IsWhite(pixColor)) { mapPixels[w * h] = new Color(0, 0, 0); } else { mapPixels[w * h] = new Color(1, 1, 1); } } } newTex.SetPixels(mapPixels); mapTex = newTex; mapTex.Apply(); } I would appriciate any feedback, I can't see what's going wrong here. Thank you.
By changing Texture2D newTex = new Texture2D(wid, hei); to Texture2D newTex = mapTex; the error is resolved. However, now I have an issue, where the mapPixels array length is 0. Could anyone enlighten me on why the mapPixels array has this length? I am trying to give it the length of wid * hei. Thanks.
I have resolved the problem. I was calling the function at the wrong time. The function works when it is called at a particular point in the procedure I am making.