Search Unity

Changing sprites color via code

Discussion in 'Scripting' started by kader1081, May 15, 2022.

  1. kader1081

    kader1081

    Joined:
    Oct 16, 2021
    Posts:
    379
    Hi ım changing sprites color the code under here but it doesn't change.When ı check sprites color in inspector it changed here but it does'nt change in scene or game

    Code (CSharp):
    1.  miner_health_bar.GetComponent<SpriteRenderer>().color= new Color(1, 2, 0, 1);
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
  3. kader1081

    kader1081

    Joined:
    Oct 16, 2021
    Posts:
    379
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Can you show a screenshot of what you're doing? That is, in editor.
     
    kader1081 likes this.
  5. kader1081

    kader1081

    Joined:
    Oct 16, 2021
    Posts:
    379
     

    Attached Files:

  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    For the health bar, is the default color a shade of pink or red? Changing the tint on a sprite that isn't white will give you a dark color like this. It doesn't recolor the sprite green.

    I don't think it being in shader graph matters. To me it just looks like it had a shade of red and a green tint was being applied to it, thus making it dark like that.
     
    kader1081 likes this.
  7. kader1081

    kader1081

    Joined:
    Oct 16, 2021
    Posts:
    379
    ı draw health bar as red.I didn't make it red in the tint
     
  8. kader1081

    kader1081

    Joined:
    Oct 16, 2021
    Posts:
    379
    I think it is not about coding or unity it is a color thing. In physics if you mix some color something happens this kind of thing
     
  9. kader1081

    kader1081

    Joined:
    Oct 16, 2021
    Posts:
    379
    ı will make the health_bar white then set it to red in unity
     
    karliss_coldwild likes this.
  10. kader1081

    kader1081

    Joined:
    Oct 16, 2021
    Posts:
    379
    Yep got it.
     
  11. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Yep, you got it. I think you understood what I was saying in the end. :D
     
    kader1081 likes this.