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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Colors not exactly matching

Discussion in 'Scripting' started by Ranbe, Jan 25, 2016.

  1. Ranbe

    Ranbe

    Joined:
    Jan 25, 2016
    Posts:
    13
    hi,
    i have 2 objects that collides , and when they are colliding i want to check if they have the same color
    i made sure to put the exact color, but for some reason it only works for 2/4 of the colors,
    and when im checking the values it says the colors are very similar 0.02 difference,
    is there a way to ignore this 0.02 , or a better way to compare colors ? ty!
    btw : renderer.material.color isn't working at all
    Code (CSharp):
    1.  
    2. public Color[] objectColor;
    3.  
    4. void changeColor()
    5.     {
    6.        currentColor = (PlayerColor)Random.Range(0, MAXCOLORS);
    7.        GetComponent<SpriteRenderer>().color = new Color(objectColor[(int)currentColor].r, objectColor[(int)currentColor].g,
    8.                                                                                    objectColor[(int)currentColor].b, objectColor[(int)currentColor].a);
    9.     }
    10.     void OnTriggerEnter2D(Collider2D other)
    11.     {
    12.         if (other.tag == "Rain")
    13.         {
    14.  
    15.             if (objectColor[(int)currentColor] == other.gameObject.GetComponent<SpriteRenderer>().color)
    16.                 FindObjectOfType<ScoreManagerController>().addPoints(2);
    17.  
    18.             Destroy(other.gameObject);
    19.         }
    20.     }
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    I wouldn't expect renderer.material.color to work on SpriteRenderer, for reasons too boring to go into right now.

    Code (csharp):
    1. public static bool ColorEquals(Color a, Color b, float tolerance = 0.04f) {
    2. if (a.r > b.r + tolerance) return false;
    3. if (a.g > b.g + tolerance) return false;
    4. if (a.b > b.b + tolerance) return false;
    5. if (a.r < b.r - tolerance) return false;
    6. if (a.g < b.g - tolerance) return false;
    7. if (a.b < b.b - tolerance) return false;
    8.  
    9. return true;
    10. }
     
    IggyZuk likes this.
  3. Ranbe

    Ranbe

    Joined:
    Jan 25, 2016
    Posts:
    13
    ty alot ! :)
     
  4. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    This is not a mere floating point imprecision problem. 0.02 is FAR bigger than epsilon.
     
  6. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    if they are the same colors they shouldn't be a 0.02 difference. if they are setted the same colors, it should be enoguh to add, subs., Epsilon.

    But your right, 0.02 is Far bigger than Epsilon