If yous've ever wanted to write a script that could make your sprite flash a dissimilar colour or plow them transparent in Unity, at that place's an piece of cake way. I did this in various ways in my game Puzzledorf. Ane example is the fireworks that explode when y'all do something right:

Or making the Press Any Central text fade in and out on the Titlescreen, or the fading push highlights on the menus:

Sprite Renderer

Y'all're sprite is contained in a component chosen the Sprite Renderer.

Sprite Renderer

Now if you click on "Color", you can manually change the sprites transparency in the color changer by turning alpha down to 0, or tint the sprite by modifying how much red, blue and green is in the paradigm.

Color Changer

Confusingly, if it's set to all white, the sprite is its normal colour, while all black volition brand the sprite black. Y'all'll discover likewise that the colours range from 0 to 255, with 0 beingness no colour and 255 being full colour.

Now unfortunately if yous turn blue and greenish off, the sprite won't be all carmine, just tinted red. If you lot want to completely change colours, you'll take to offset with a white sprite, otherwise it will just tint.

Coding Color Changes

To modify the colour in a script, you will need to get theSprite Renderer in your lawmaking, and then we tin can easily change it'southward color property.(Color is the American spelling whereas colour is the Australian spelling).

Create a scene with a game object that has a sprite renderer / sprite attached. Then create a new script called ColourChange and add the following code:

              SpriteRenderer sprite;                      void Start()                      {                      sprite = GetComponent<SpriteRenderer>();                      }                      void Update()                      {                      if(Input.GetKeyDown(KeyCode.C))                      {                                              // Alter the 'colour' belongings of the 'Sprite Renderer'                                sprite.color = new Color (1, 0, 0, 1);                                }                      }        

The very first thing we exercise is create a variable to store theSprite Renderer, and we call thissprite. And so inOutset() nosotros go the component for theSprite Renderer, and so this fashion we can access it'scolour belongings. If your sprite is in a child game object of where you attached this script, instead ofGetComponent, useGetComponentInChildren like this:

          sprite = GetComponentInChildren<SpriteRenderer>();        

At present inUpdate() we outset of all check to see if the centralC is being pressed on the keyboard. If y'all're confused with how that works, read the Unity documentation on information technology hither; it's helpful.

And so we change thecolour property of ourSprite Renderer by creating a newColour. This literally just means we're changing those same color properties from before, just with lawmaking. It looks like this:

          sprite.color = new Colour (Ruby-red, Green, Bluish, Alpha);        

Confusingly, instead of being a value between 0 and 255, like it was in the editors color changer, now we have to give it a value between 0 and i for each colour property, 0 being no colour, ane being full colour for that belongings.

So in other words, if yous run your game and test it now, it will tint your sprite ruddy, considering we told it to be:

  • Full Cherry
  • No Green
  • No Blue
  • Fully Visible

Also if we changed our colour holding like this:

          sprite.colour = new Color (0, 1, 0, ane);        

It would tint our sprite green. Try it.

Then if you did this:

sprite.colour = new Color (1, ane, 1, 0);

Your sprite would exist its normal colour, but it would be transparent, because we turned the alpha all of the way down. So that'due south a nice way to make your sprite invisible, but nevertheless enabled.

Yous should be able to use this to create some dainty effects in your games now like a graphic symbol flashing when information technology'due south hurt.