Getting correct pixel color (solved)

Problem: An unexpected value is observed when reading the Color32 pixel color of a 2D texture
sprite.
EDIT: SOLVED
SOLUTION: the problem was in the dictionary the alpha channel was set to zero. In the script I check for an alpha of zero (if so then no object is created). Changing the dictionary to reflect an alpha of greater than zero solved, the issue

Black pixels are read correctly and produce wall prefabs. But the 1 red pixel reads as a white pixel when observed in the debug console.

//EXPECTED VALUE
//c: RGBA(255, 0, 0, 0)

c: RGBA(0, 0, 0, 0)
UnityEngine.Debug:Log(Object)
LevelGenerator:GenerateTile(Color32, Int32, Int32) (at Assets/scripts/LevelGenerator.cs:30)
LevelGenerator:GenerateLevel() (at Assets/scripts/LevelGenerator.cs:23)
LevelGenerator:Start() (at Assets/scripts/LevelGenerator.cs:11)

Below is the code generating a level

// this is the color dictionary
public class ColorToPrefab {
    public Color32 color;
    public GameObject prefab;
}
// this is the generator script
    public Texture2D map;
    public ColorToPrefab[] colorMappings;

    void Start () {
        GenerateLevel();
    }

    void GenerateLevel ()
    {
        Color32[] pColor = map.GetPixels32 (); // I store pixels in a list
        for (int x = 0; x < map.width; x++) // then go through the list
        {
            for (int y = 0; y < map.height; y++)
            {
                Debug.Log ("* * * * *   GeneratingTile --> x: " + x + ", y: " + y+"    * * * * *");
                int index = x + y * map.width;
                GenerateTile(pColor[index],x, y); // and generate the tile
            }
        }
     }

    void GenerateTile (Color32 c, int x, int y)
    {
        Debug.Log ("c: "+c);
        if (c.a == 0) {
            // The pixel is a path. Let's ignore it!
            Debug.Log("PATH FOUND -RETURN-");
            return;
        }

        foreach (ColorToPrefab colorMapping in colorMappings) // I go through each index in the dictionary
        {
            Debug.Log ("ColorMapping.color: "+colorMapping.prefab.name);
            if (colorMapping.color.Equals(c)) // and see if the pixel color matches a dictionary color
            {
                Debug.Log ("ColorMapping.color == pixelColor --> CREATING OBJECT");
                Vector3 position = new Vector3(x, 1, y);
                Instantiate(colorMapping.prefab, position, Quaternion.identity, transform);
                return;
            }
        }
    }

I’ve uploaded the sprite and below is the sprite’s Meta data

fileFormatVersion: 2
guid: 0adda6bc42ae9234f88f40448e037606
timeCreated: 1504362991
licenseType: Free
TextureImporter:
  fileIDToRecycleName: {}
  serializedVersion: 4
  mipmaps:
    mipMapMode: 0
    enableMipMap: 0
    sRGBTexture: 1
    linearTexture: 0
    fadeOut: 0
    borderMipMap: 0
    mipMapsPreserveCoverage: 0
    alphaTestReferenceValue: 0.5
    mipMapFadeDistanceStart: 1
    mipMapFadeDistanceEnd: 3
  bumpmap:
    convertToNormalMap: 0
    externalNormalMap: 0
    heightScale: 0.25
    normalMapFilter: 0
  isReadable: 1
  grayScaleToAlpha: 0
  generateCubemap: 6
  cubemapConvolution: 0
  seamlessCubemap: 0
  textureFormat: 1
  maxTextureSize: 2048
  textureSettings:
    serializedVersion: 2
    filterMode: 0
    aniso: 1
    mipBias: -1
    wrapU: 1
    wrapV: 1
    wrapW: 1
  nPOTScale: 0
  lightmap: 0
  compressionQuality: 50
  spriteMode: 1
  spriteExtrude: 1
  spriteMeshType: 0
  alignment: 0
  spritePivot: {x: 0.5, y: 0.5}
  spriteBorder: {x: 0, y: 0, z: 0, w: 0}
  spritePixelsToUnits: 100
  alphaUsage: 1
  alphaIsTransparency: 1
  spriteTessellationDetail: -1
  textureType: 8
  textureShape: 1
  maxTextureSizeSet: 0
  compressionQualitySet: 0
  textureFormatSet: 0
  platformSettings:
  - buildTarget: DefaultTexturePlatform
    maxTextureSize: 2048
    textureFormat: -1
    textureCompression: 0
    compressionQuality: 50
    crunchedCompression: 0
    allowsAlphaSplitting: 0
    overridden: 0
  - buildTarget: Standalone
    maxTextureSize: 2048
    textureFormat: -1
    textureCompression: 0
    compressionQuality: 50
    crunchedCompression: 0
    allowsAlphaSplitting: 0
    overridden: 0
  - buildTarget: WebGL
    maxTextureSize: 2048
    textureFormat: -1
    textureCompression: 0
    compressionQuality: 50
    crunchedCompression: 0
    allowsAlphaSplitting: 0
    overridden: 0
  spriteSheet:
    serializedVersion: 2
    sprites: []
    outline: []
    physicsShape: []
  spritePackingTag:
  userData:
  assetBundleName:
  assetBundleVariant:

3206231--245270--Level1_vers1.png

The actual debug log looks like this for the tile location

* * * * *   GeneratingTile --> x: 30, y: 15    * * * * *
c: RGBA(0, 0, 0, 0)
PATH FOUND -RETURN-

You say it reads as a white pixel, but the actual debug output shows black. So, that’s curious.

Also, could it be the pixels are not in the order you think they are? What if you give it a texture filled ENTIRELY with red pixels? Do they all show as black, or what?

1 Like

Thank you Joe, your solution was the trick. 1st where I thought the red pixel was located was not where it was located at all. Instead of [30,15] where I thought it should be as indicated on the sprite, it was at [15,1]. Strange. And secondly, giving the script with walls having red pixels as walls showed the flaw. In the dictionary I had the color correct, it just had (0) for an alpha value (255,0,0,0)

EDIT:
oh the [30,15] tile was a path tile (0 for alpha) whether it be black or white. It’s the alpha channel that correlates the default to path (bare of any object)

1 Like