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

Change SpriteRenderer Color Using Script

Discussion in 'Scripting' started by SheldonS, Oct 12, 2016.

  1. SheldonS

    SheldonS

    Joined:
    Jul 23, 2015
    Posts:
    21
    I am trying to change the color of a sprite. I do not know if this matters, but it is animated and the child of another object. I am able to kind of sort of change the color but something is not right. I have tried a few things guided by threads I found in the forums, but it still is not 100% correct.

    I want to change the color to one of three based on a condition; yellow, orange, red. To do this I created some private variables including their alpha.

    Code (CSharp):
    1. private Color shieldYellow = new Color(Color.yellow.r, Color.yellow.g, Color.yellow.b, 125.0f);
    2.     private Color shieldOrange = new Color(255.0f, 153.0f, 51.0f, 125.0f);
    3.     private Color sheildRed = new Color(Color.red.r, Color.red.g, Color.red.b, 125.0f);
    When I play the game and the conditions are met the colors does change. For Yellow and Red the alpha value is ignored and gets set to 255. For Orange everything is ignored and the normal white color of the original sprite is displayed.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You need to post relevant code.

    --Eric
     
  3. SheldonS

    SheldonS

    Joined:
    Jul 23, 2015
    Posts:
    21
    I will post the code where I set the spriterenderer's color to one of my colors as soon as I can; I am back at work.
     
  4. SheldonS

    SheldonS

    Joined:
    Jul 23, 2015
    Posts:
    21
    Here is the code for the Shield class.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Shield : MonoBehaviour
    5. {
    6.     public int initialHitPoints = 30;
    7.  
    8.     private int hitPoints;
    9.     private SpriteRenderer spriteRender;
    10.     private Color shieldYellow = new Color(Color.yellow.r, Color.yellow.g, Color.yellow.b, 125.0f);
    11.     private Color shieldOrange = new Color(255.0f, 153.0f, 51.0f, 125.0f);
    12.     private Color sheildRed = new Color(Color.red.r, Color.red.g, Color.red.b, 125.0f);
    13.    
    14.     public void RefreshShield()
    15.     {
    16.         spriteRender = GetComponentInChildren<SpriteRenderer>();
    17.         hitPoints = initialHitPoints;
    18.         spriteRender.color = shieldYellow;
    19.     }
    20.  
    21.     public virtual void CalculateHit(int amount)
    22.     {
    23.         hitPoints -= amount;
    24.         spriteRender.color = DetermineShieldColor();
    25.         if (hitPoints <= 0)
    26.         {
    27.             gameObject.SetActive(false);
    28.         }
    29.     }
    30.  
    31.     private Color DetermineShieldColor()
    32.     {
    33.         if(hitPoints < initialHitPoints && hitPoints > 10)
    34.         {
    35.             return shieldOrange;
    36.         }
    37.         if(hitPoints <= 10)
    38.         {
    39.             return sheildRed;
    40.         }
    41.         return shieldYellow;
    42.     }
    43. }
    44.  
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Actually, I just looked at the original post again and realized that was enough, sorry. You're treating Color like Color32 (except with floats), but they don't work the same at all.

    --Eric
     
  6. SheldonS

    SheldonS

    Joined:
    Jul 23, 2015
    Posts:
    21
    Thank you. I thought I had looked at the Color properly but upon further investigation and reading I found what works.

    Code (CSharp):
    1. private Color shieldYellow = new Color(Color.yellow.r, Color.yellow.g, Color.yellow.b, .5f);
    2.     private Color shieldOrange = new Color(1, .3f, .1f, .4f);
    3.     private Color shieldRed = new Color(Color.red.r, Color.red.g, Color.red.b, .3f);