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 How can I use color that way...

Discussion in 'Scripting' started by LazyGhost15, Jun 6, 2023.

  1. LazyGhost15

    LazyGhost15

    Joined:
    Feb 12, 2022
    Posts:
    88
    I made 2 tiles with different .r values and I asked the console to read each tile .r value and I all was 1. I know I used 2 different tiles because of the distinct different between the 2 tiles. and I know I gave them 2 different R values when I made them (the R or .r is the red in rgb) in aseprite (an image editing application to make 2D sprites)
     
  2. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    You need to show code. I don't care what you believe you did, but what you actually did. If you think there is something wrong with your results, it's probably due to a mistake.

    Nobody wants to have bugs in their code, so it has to be due to mistakes being made, no? And if everyone makes mistakes, let's start from there. Show us the code that is 100% sure to contain a mistake, then describe that with your flawless reasoning. Not the other way around.

    Oh and please make sure to use code tags for your code.
     
  3. LazyGhost15

    LazyGhost15

    Joined:
    Feb 12, 2022
    Posts:
    88
    Ok so this is my code:

    Code (CSharp):
    1.  public void GetTileColor()
    2.     {
    3.         for(int x = -100; x < rows; x++)
    4.         {
    5.             for(int z = -100; z < cols; z++)
    6.             {
    7.                 if(tilemap.GetSprite(new Vector3Int(x, z , 0)) != null)
    8.                 {
    9.                     Color SpriteColor = tilemap.GetColor(new Vector3Int(x, z, 0));
    10.                     GenerateCube(new Vector3(x , 0, z), SpriteColor);
    11.                 }
    12.                 else if(tilemap.GetSprite(new Vector3Int(x, 0, z)) == null)
    13.                 {
    14.                     //print(x + " , " + z + " null");
    15.                 }
    16.             }
    17.         }
    18.     }
    19.  
    20.     void GenerateCube(Vector3 position, Color NeededColor)
    21.     {
    22.         Vector3 ObjPosition;
    23.         Color32 Thecolor = NeededColor;
    24.         for (int y = 0; y < Thecolor.r; y++)
    25.         {
    26.             ObjPosition = new Vector3(position.x + 0.5f, y + 1, position.z + 0.5f);
    27.            // Instantiate(Cube, ObjPosition, transform.rotation, Parent1.transform);
    28.             if(Thecolor.r > 1)
    29.             {
    30.                 Debug.Log(Thecolor.r);
    31.             }
    32.         }
    33.  
    34.     }
    As you can see, the code gets the sprite color, and send it to another function which writes the color .r value. Originally it should have made cubes and decided if to put them at y 0 or y 1. but after all the cubes had the same y I changed it so it would just write the .r value.
     
  4. dynamicbutter

    dynamicbutter

    Joined:
    Jun 11, 2021
    Posts:
    61
    @LazyGhost15 should lines 28 and 30 be "y" instead of "Thecolor.r"?
     
  5. LazyGhost15

    LazyGhost15

    Joined:
    Feb 12, 2022
    Posts:
    88
    I don't think so cuz I want to print the R value of the tile. Not the y position.
     
  6. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Ok, be mindful of the difference between Color and Color32. These are not the same thing.

    Color32 is the actual, cold data type where each channel (RGBA) represents the underlying 8-bit value, leading to a 32-bit value (4x8).

    Color is an abstract color data type, which has its uses across various color data formats (for example HDR), and is basically hardware-independent representation. Here each channel (RGBA) is represented as a floating point value between 0 and 1, where 1 is just some arbitrary maximum (for example 255 in a 8-bit system).

    We actually use Color throughout in Unity, unless a specific use case arises, but you need to be aware that a pure numeric conversion might fail.

    Edit: Color32 is actually a tight representation of the true 32-bit color system (also known as true color), and the struct will actually consume 4 bytes of memory (+ overhead). Color, on the other hand, consumes 4 bytes of memory per channel, thus 16 bytes of memory, making it much more memory greedy, and unsuitable for large scale sampling and storage. This is just TIL, not relevant for your case.
     
    Last edited: Jun 6, 2023
    LazyGhost15 likes this.
  7. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    That said, I believe that your error is in line 23 where you declare a Color32. Why though? Your argument is already Color. But this is not where this fails, thanks to implicit conversions. It fails later at line 28 (and probably at 24 as well, I'm too lazy to run this code so I'm just extrapolating what might happen).

    Try debugging these values, and see for yourself.
     
  8. LazyGhost15

    LazyGhost15

    Joined:
    Feb 12, 2022
    Posts:
    88
    Ok, i'll try to make all the values color and then all color32 and see what happens
     
  9. LazyGhost15

    LazyGhost15

    Joined:
    Feb 12, 2022
    Posts:
    88
    Ok I did that, this is what happend:
    When I made all the color variables color 32 the game was busy for 2 minutes before it crashed.
    When I made all the color32 variables color only, the console returned only "1" (I disabled the if .r > 1)
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    You're laboriously making 100x100 things... each of which could have up to 255 inner steps...

    Simplify, simplify, simplify... try making 2x2 for now until you figure out your other bugs.
     
  11. LazyGhost15

    LazyGhost15

    Joined:
    Feb 12, 2022
    Posts:
    88
    Ok i'll change the map size to 2x2 and try again
     
  12. LazyGhost15

    LazyGhost15

    Joined:
    Feb 12, 2022
    Posts:
    88
    I tried doing the same thing in a 2x2 instead of 200x200 but I got the same results. the only difference was that this time it took 6 minutes before crash.
     
  13. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Pay attention to your start values... if you set rows/cols to 2 and 2, you're STILL doing 102 x 102

    You should endeavor to iterate from 0 to n-1 rather than whacky negative starts.
     
  14. LazyGhost15

    LazyGhost15

    Joined:
    Feb 12, 2022
    Posts:
    88
    Yeah, I changed that to 0 since it's for testing.