So I'm trying to use GetPixel() function to get which colors is the texture as a value of x and y. So far it works well but it only return 1 color, being unable to get the rest of the colors of the copied texture. Example texture Let's consider this texture, my one is quite similar, I'll update as soon as I can. But it will work as example, basically, GetPixel() is only returning the red value while the rest are like doesn't exist at all. I leave here the code: EDITED CODE

public Texture2D oldTexture = GetDistortedGradient(1f);

public Texture2D GetTexture(){
   var texture = new Texture2D(width, height);
   var pixels = new Color[width * height];

   for(var x = 0; x < width; x++){
      for(var y = 0; y < height; y++){
         Color newPixels = oldTexture.GetPixel(x, y);

         if(newPixels == hotest){
            pixels[x + y * width] = Color.red;
         else if (newPixels == coldest){
            pixels[x + y * width] = Color.blue;
            pixels[x + y * width] = Color.white;


   return texture;

What I'm trying to do is whenever that texture is "color1" set the new texture as a red color on the specified x and y coordinate, then check again and if the texture is "color2" on the news x and y, set to blue, and so on. However, this is only returning color1 for the correctly x, y coordinates and after that it jumps to the else function, like if color2 never existed on the old texture. Btw, as this is an example, the oldTexture is generated procedurally but the colors are set in the inspector, so I just take the color I've set, but i don't why it only keep returning 1 of many colors, when basically it check for all colors. Am I missing anything or am I not understanding GetPixel() function and how it work? Any help will be appreciated!


public Texture2D GetDistortedGradient(float noiseStrength)
    var texture = new Texture2D(heatMap.Width, heatMap.Height);
    var pixels = new Color[heatMap.Width * heatMap.Height];
    for (var x = 0; x < heatMap.Width; x++)
        for (var y = 0; y < heatMap.Height; y++)
             float noise = GetNoise(x, y);
             int shift = Mathf.RoundToInt(noise * noiseStrength);
             pixels[x + y * heatMap.Width] = GetGradientColor(x, y + shift);
    //And apply texture ofc

//And here the colors to get

Color GetGradientColor(int x, int y) {
if (y <= southPoleCap || y >= northPoleCap)
    return coldest;
else if (y > southPoleCap && y <= southArcticCap || y < northPoleCap && y >= northArcticCap)
    return colder;
else if (y > southArcticCap && y <= southArcticOuter || y < northArcticCap && y >= northArcticOuter)
    return cold;
else if (y > southArcticOuter && y <= southMiddleOuter || y < northArcticOuter && y >= northMiddleOuter)
    return template;
else if (y > southMiddleOuter && y <= southTropic || y < northMiddleOuter && y >= northTropic)
    return hot;
else if (y > southTropic && y <= southMiddleInner || y >= northMiddleInner && y < northTropic)
    return hotter;
    return hottest;

I'm not at home right now, so I can't give the new code I've got for that, but that's the proper base, just few really small changes So, once the GetDistortedGradient texture is done, it should go to the GetTexture and create based on whatever colors there's on the distorted texture

  • \$\begingroup\$ The Unity Color struct encodes colors as floating point values. So it could be the age-old problem of checking floating point values for equality. Maybe use Color32 and oldTexture.GetPixels32() instead? \$\endgroup\$
    – Philipp
    Aug 20, 2019 at 8:58
  • \$\begingroup\$ i was getting info about that these days, and is more than probably that's the problem, I'll check it later as know I don't have the proper equipment, but thanks! so basically, i just need to compare every value of the color, and maybe do a mathf approximate? I didn't check for GetPixel32() but I'll give it a try \$\endgroup\$
    – OverGast
    Aug 20, 2019 at 9:01
  • \$\begingroup\$ Can you include your procedural generation of oldTexture here? It might be relevant if the texture is generated in such a way that its pixels aren't readable by the time this script runs. \$\endgroup\$
    – DMGregory
    Aug 20, 2019 at 11:44
  • \$\begingroup\$ Hey! I just made an update to add the code, because I can't right now I just took what we did in the following link, basically I was doing other scripting these weeks, but I came back to a similar thing. By the way, I'm the same guy that asked that question xD Check if you feel to \$\endgroup\$
    – OverGast
    Aug 20, 2019 at 12:11
  • \$\begingroup\$ Btw, say that I think the new texture is generated before that the oldTexture, so maybe that's causing another problem too. Anyway, I tried to change which texture is generated in which order, but always getting the same output \$\endgroup\$
    – OverGast
    Aug 20, 2019 at 12:40


You must log in to answer this question.

Browse other questions tagged .