Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Tilemap is invisible white.....

Discussion in '2D' started by LazyGhost15, Oct 7, 2023.

  1. LazyGhost15

    LazyGhost15

    Joined:
    Feb 12, 2022
    Posts:
    88
    So I made a script that gets the color of the pixel in the texture2D, and then it places the color at the tile by order, I used some debugging and it looks like it goes through every stage and just the SetTile isn't working, It has the color and the location of the tile. This is the script I used:
    Code (CSharp):
    1. Color Pcolor = PNGMap.GetPixel(x, z);
    2.         print(Pcolor.a);
    3.         if(Pcolor.a == 0)
    4.         {
    5.             print("No Color");
    6.             return;
    7.         }
    8.         else
    9.         {
    10.             print("The Last Step");
    11.             TMap.SetColor(new Vector3Int(x, 0, z), Pcolor);
    12.         }
    13.     }
    Then someone offered to reset the flags, like so:
    Code (CSharp):
    1. var pos = new Vector3Int(x, 0, z);
    2. TMap.SetTileFlags(pos, TileFlags.None);
    3. TMap.SetTileFlags(pos, TMap.GetTileFlags(pos) & ~TileFlags.LockColor);
    4. TMap.SetColor(pos, Pcolor);
    But that didn't work either. So I did more debugging and I found out that the tile's color is 225 in every aspect, so it should be white. But I can't see the tile has any color:


    And now I'm stuck.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,711
    The color in your Debug.Log() snippet above is white. This is multiplicatively applied to the graphics of the tile, so in this context it is an identity operation: there will be no output change.

    I'll assume you mean 255. This is not a valid input to Color.

    Color() accepts channel params from 0.0f to 1.0f

    Color32() accepts channel params from 0 to 255

    More on Color vs Color32, lossless, comparisons, etc.

    https://forum.unity.com/threads/color32-should-have-an-method.1323399/#post-8367126


    I also just checked: colorizing tiles works all day long, but the flag clear is necessary, otherwise the color change is ignored.

    Here is my test script:

    Code (csharp):
    1. using UnityEngine;
    2. using UnityEngine.Tilemaps;
    3.  
    4. // @kurtdekker - drop it into a scene with a Tilemap
    5. // it will apply random colors to random tiles around
    6. // a box centered at (0,0,0).
    7.  
    8. public class RandoColor : MonoBehaviour
    9. {
    10.     void Update()
    11.     {
    12.         var tmap = FindObjectOfType<Tilemap>();    // obviously don't do this every frame!
    13.  
    14.         Vector3Int position = new Vector3Int(
    15.                  Random.Range(-40, +40),
    16.                  Random.Range(-30, +30),
    17.                  0);
    18.  
    19.         Color color = Random.ColorHSV();
    20.  
    21.         var flags = tmap.GetTileFlags(position);
    22.         flags &= ~TileFlags.LockColor;
    23.         tmap.SetTileFlags(position, flags);
    24.  
    25.         tmap.SetColor(position, color);
    26.     }
    27. }
    Screenshot 2023-10-07 at 12.28.46 PM.png
     
  3. sildeflask

    sildeflask

    Joined:
    Aug 16, 2023
    Posts:
    155
    instead of using set color

    change the color with tile.color and then use tmpa.settile
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,507
  5. LazyGhost15

    LazyGhost15

    Joined:
    Feb 12, 2022
    Posts:
    88
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,507
  7. LazyGhost15

    LazyGhost15

    Joined:
    Feb 12, 2022
    Posts:
    88
    Tried to clear all flags. Still didn't work...
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,711
  9. LazyGhost15

    LazyGhost15

    Joined:
    Feb 12, 2022
    Posts:
    88
    Tried that on a new scene with a new script in a new grid, still hasn't worked...
     
  10. LazyGhost15

    LazyGhost15

    Joined:
    Feb 12, 2022
    Posts:
    88
    Package of what I did:
     

    Attached Files:

  11. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,507
    I'm a little confused here; if the above package is supposed to demonstrate something, may I ask why it's an empty tilemap with the script we've already seen? Should it not be a populated tilemap running the script? Sure, we can go ahead and find some tiles, paint them then run your script but isn't the point here to provide something that should work but doesn't on your machine?

    Also, you didn't mention it was rotated and an XZ tilemap etc so it was super confusing at first as nothing was appearing. Would've been good to mention that and TBH, not sure why it was necessary for this.

    Regardless, I grabbed a while tile, painted it and ran your script and the tiles went blue:
    color.png

    I ran it on several versions of Unity and I get the same thing. Maybe there's a bug somewhere but unfortunately the package hasn't really made anything clearer TBH.
     
  12. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,507
    Another thing I wonder if you're hitting is that I can see you're doing this at edit time and not at runtime and I wonder if those tiles (which are locked) are resetting their default colors.

    Note that you can change the flags of any/all tiles by changing the "Inspector" to Debug mode:
    Debug.png

    ... then selecting your tile asset as seen here you can uncheck the lock-color flag by by default so it won't ever have its color locked, even in edit mode so you can just set the color without messing with flags.
    LockColor.png