Search Unity

Changing All of the Animation PNGs color. is it possible?

Discussion in '2D' started by Onix, Oct 29, 2021.

  1. Onix

    Onix

    Joined:
    Feb 24, 2014
    Posts:
    6
    Hello everyone

    EDIT:
    I actually found the issue.
    Apparently you cant change the Sprite rendered color from Black to white or any other color but you can do the opposite. so what i did is changing the color from all of my Animations to white and now i can switch colors without any issues.


    So i'm fairly new to Unity and i'm trying to do the following:-

    When I Press Q my GameObject changes color to Black and when I Press W it change the color to white. its fairly easy to do so and I went with this code:-

    Code (CSharp):
    1.     void Update()
    2.     {
    3.  
    4.         if (Input.GetKeyDown(KeyCode.Q)) //Call White
    5.         player.GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f, 1f);
    6.  
    7.         if (Input.GetKeyDown(KeyCode.W)) // Call Black
    8.         player.GetComponent<SpriteRenderer>().color = new Color(0f, 0f, 0f, 1f);
    9.     }
    Worked Perfectly using a 2D GameObject I added from Unity but now when I Started adding my game graphics I have an issue were my PNGs doesn't change color at all. and I figured out that the issue is the Animation that I added to the game because without the Animation playing if i added 1 PNG file and Attached the code to it. it actually works.

    Code (CSharp):
    1. public class ChangeColor : MonoBehaviour
    2. {
    3.     public GameObject player;
    4.     void Awake()
    5.     {
    6.  
    7.         player.GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f, 1f);
    8.     }
    9.  
    10.  
    11.     void Update()
    12.     {
    13.         if (Input.GetKeyDown(KeyCode.Q)) //Call White
    14.             player.GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f, 1f);
    15.          
    16.  
    17.         if (Input.GetKeyDown(KeyCode.W)) // Call Black
    18.             player.GetComponent<SpriteRenderer>().color = new Color(0f, 0f, 0f, 1f);
    19.     }
    20. }
    But when adding the Animations the Original PNG color will Play and that makes sense i guess since the Animator will use the PNG source files to play the Animation.

    My question: Is there a way to change the Sprites of the Animation color when i Press Q or W from unity instead of making duplicate Animations for every single Animation i want to play and make one of them white and the other is black? there must be a way but i'm missing it.

    Thanks alot.
     
    Last edited: Nov 1, 2021
  2. Onix

    Onix

    Joined:
    Feb 24, 2014
    Posts:
    6
    any help guys ? i'm disparate:(
     
  3. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,447
    are you animating the color in animator? (that would then override your script color)
     
  4. Onix

    Onix

    Joined:
    Feb 24, 2014
    Posts:
    6
    thank you for trying to help. I found the issue and fixed it.

    Apparently you cant change the Sprite rendered color from Black to white or any other color but you can do the opposite. so what i did is changing the color of all of my Animations to white and now i can switch colors without any issues.