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

how do I add to the rgb value in the sprite renderer?

Discussion in '2D' started by omin0uschaos, Oct 21, 2015.

  1. omin0uschaos

    omin0uschaos

    Joined:
    Oct 21, 2015
    Posts:
    4
    Hi,

    This is the bit of code i have so far.. I know i've made a mistake somewhere . My desired result is to have the object flash before its destroyed.


    .
    Code (CSharp):
    1. this.GetComponent<SpriteRenderer>.color = + .15f, + .15f, + .15f, 1f;
    Thanks in advance for any help.
     
  2. IntDev

    IntDev

    Joined:
    Jan 14, 2013
    Posts:
    152
    try this

    Code (CSharp):
    1. this.GetComponent<SpriteRenderer>.color = new Color(.15f, .15f, .15f, 1f);
     
    omin0uschaos likes this.
  3. omin0uschaos

    omin0uschaos

    Joined:
    Oct 21, 2015
    Posts:
    4
    that didn't do the trick, it just changes the bricks to a different color. (almost black) I need it to make the object appear to flash and then either go back to normal or get destroyed.
     
  4. omin0uschaos

    omin0uschaos

    Joined:
    Oct 21, 2015
    Posts:
    4
    Code (CSharp):
    1. void OnCollisionEnter2D (Collision2D collision)    {
    2.     AudioSource.PlayClipAtPoint(crack, transform.position, 0.2f);
    3.         this.GetComponent<SpriteRenderer>().color = new Color(+ .15f, + .15f, + .15f, 1f);
    4.     if (isBreakable) {
    5.         HandleHits();
    6.         }
    7.  
    8. }
    9.  
    10.     void HandleHits () {
    11.        
    12.         timesHit ++;
    13.         int maxHits = hitSprites.Length + 1;
    14.         if (timesHit >= maxHits){
    15.             breakableCount--;
    16.             levelManager.BrickDestroyed();
    17.             PuffSmoke();
    18.             Destroy(gameObject);
    19.  
    20.             print(breakableCount + " Blocks Left");
    21.         }
    22.         else {
    23.             LoadSprites();
    24.         }
    25.     }
    Heres more of the code in case it helps...
     
  5. Raclette

    Raclette

    Joined:
    Oct 16, 2012
    Posts:
    15
    Have you tried that?
    Code (CSharp):
    1. public Color myColor = new Color();
    Then on the inspector you can choose your color.
    And then you add this on your OnCollisionEnter2D script.
    Code (CSharp):
    1. this.GetComponent<SpriteRenderer>().color = myColor;
     
    Last edited: Oct 26, 2015
    omin0uschaos likes this.
  6. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    If you intend add to the current color, you need to do something like this:

    Code (CSharp):
    1.         Color c = GetComponent<SpriteRenderer>().color;
    2.         c.r += .15f;
    3.         c.g += .15f;
    4.         c.b += .15f;
    5.         GetComponent<SpriteRenderer>().color = c;
    6.  
     
    omin0uschaos likes this.
  7. omin0uschaos

    omin0uschaos

    Joined:
    Oct 21, 2015
    Posts:
    4
    this worked beautifully... thank you so much. Also thanks to everyone that posted. I appreciate the help.