<?xml version="1.0" encoding="UTF-8" standalone="yes"?><oembed><version><![CDATA[1.0]]></version><provider_name><![CDATA[Stuart&#039;s Pixel Games]]></provider_name><provider_url><![CDATA[http://stuartspixelgames.com]]></provider_url><author_name><![CDATA[SPG]]></author_name><author_url><![CDATA[https://stuartspixelgames.com/author/stuie89/]]></author_url><title><![CDATA[How To Change Sprites Colour Or Transparency &#8211; Unity&nbsp;C#]]></title><type><![CDATA[link]]></type><html><![CDATA[<div class="wp-block-image">
<figure class="aligncenter size-large"><a href="https://www.humblebundle.com/g/puzzledorf"><img data-attachment-id="7638" data-permalink="https://stuartspixelgames.com/wide-ad/" data-orig-file="https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png" data-orig-size="1920,586" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="wide-ad" data-image-description="" data-image-caption="" data-medium-file="https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=300" data-large-file="https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=1024" src="https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=736" alt="" class="wp-image-7638" srcset="https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=736 736w, https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=1472 1472w, https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=150 150w, https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=300 300w, https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=768 768w, https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=1024 1024w" sizes="(max-width: 736px) 100vw, 736px" /></a></figure></div>


<p>If you&#8217;ve ever wanted to write a script that could make your sprite flash a different colour or turn them transparent in Unity, there&#8217;s an easy way. I did this in various ways in my game <strong><a href="https://www.humblebundle.com/g/puzzledorf">Puzzledorf</a></strong>. One example is the fireworks that explode when you do something right:</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><a href="https://www.humblebundle.com/g/puzzledorf"><img data-attachment-id="5636" data-permalink="https://stuartspixelgames.com/blockoncrossexplosion/" data-orig-file="https://stuartspixelgames.files.wordpress.com/2021/09/blockoncrossexplosion.gif" data-orig-size="423,365" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="blockoncrossexplosion" data-image-description="" data-image-caption="" data-medium-file="https://stuartspixelgames.files.wordpress.com/2021/09/blockoncrossexplosion.gif?w=300" data-large-file="https://stuartspixelgames.files.wordpress.com/2021/09/blockoncrossexplosion.gif?w=423" src="https://stuartspixelgames.files.wordpress.com/2021/09/blockoncrossexplosion.gif?w=423" alt="" class="wp-image-5636" /></a></figure></div>


<p>Or making the <strong>Press Any Key</strong> text fade in and out on the <strong>Titlescreen</strong>, or the fading button <strong>highlights </strong>on the <strong>menus</strong>:</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><a href="https://www.humblebundle.com/g/puzzledorf"><img data-attachment-id="6156" data-permalink="https://stuartspixelgames.com/2020/08/13/how-to-detect-any-key-press-but-not-mouse-unity-c/menutitlescreenanimated-2/" data-orig-file="https://stuartspixelgames.files.wordpress.com/2020/08/menutitlescreenanimated-2.gif" data-orig-size="666,369" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="MenuTitleScreenAnimated 2" data-image-description="" data-image-caption="" data-medium-file="https://stuartspixelgames.files.wordpress.com/2020/08/menutitlescreenanimated-2.gif?w=300" data-large-file="https://stuartspixelgames.files.wordpress.com/2020/08/menutitlescreenanimated-2.gif?w=666" src="https://stuartspixelgames.files.wordpress.com/2020/08/menutitlescreenanimated-2.gif?w=666" alt="" class="wp-image-6156" /></a></figure></div>


<p><strong>Sprite Renderer</strong></p>



<p>You&#8217;re sprite is contained in a component called the <strong>Sprite Renderer</strong>.</p>



<figure class="wp-block-image"><img data-attachment-id="4653" data-permalink="https://stuartspixelgames.com/2019/02/19/how-to-change-sprites-colour-or-transparency-unity-c/sprite-renderer/" data-orig-file="https://stuartspixelgames.files.wordpress.com/2019/02/sprite-renderer.png" data-orig-size="369,198" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Sprite Renderer" data-image-description="" data-image-caption="" data-medium-file="https://stuartspixelgames.files.wordpress.com/2019/02/sprite-renderer.png?w=300" data-large-file="https://stuartspixelgames.files.wordpress.com/2019/02/sprite-renderer.png?w=369" src="https://stuartspixelgames.files.wordpress.com/2019/02/sprite-renderer.png" alt="Sprite Renderer" class="wp-image-4653" /></figure>



<p>Now if you click on &#8220;Color&#8221;, you can manually change the sprites transparency in the colour changer by turning <strong>alpha</strong> down to 0, or tint the sprite by modifying how much <strong>red</strong>, <strong>blue</strong> and <strong>green</strong> is in the image.</p>



<figure class="wp-block-image"><img data-attachment-id="4654" data-permalink="https://stuartspixelgames.com/2019/02/19/how-to-change-sprites-colour-or-transparency-unity-c/color-changer/" data-orig-file="https://stuartspixelgames.files.wordpress.com/2019/02/color-changer.png" data-orig-size="231,494" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Color Changer" data-image-description="" data-image-caption="" data-medium-file="https://stuartspixelgames.files.wordpress.com/2019/02/color-changer.png?w=140" data-large-file="https://stuartspixelgames.files.wordpress.com/2019/02/color-changer.png?w=231" src="https://stuartspixelgames.files.wordpress.com/2019/02/color-changer.png" alt="Color Changer" class="wp-image-4654" /></figure>



<p>Confusingly, if it&#8217;s set to all white, the sprite is its normal colour, while all black will make the sprite black. You&#8217;ll notice also that the colours range from 0 to 255, with 0 being no colour and 255 being full colour.</p>



<p>Now unfortunately if you turn blue and green off, the sprite won&#8217;t be all red, just tinted red. If you want to completely change colours, you&#8217;ll have to start with a white sprite, otherwise it will just tint.</p>



<h2 id="coding-colour-changes"><strong>Coding Colour Changes</strong></h2>



<p>To change the colour in a script, you will need to get the&nbsp;<strong>Sprite Renderer</strong> in your code, and then we can easily change it&#8217;s <strong>color</strong> property.&nbsp;<strong>(Color</strong> is the American spelling whereas <strong>colour</strong> is the Australian spelling).</p>



<p>Create a scene with a game object that has a <strong>sprite renderer</strong> / <strong>sprite</strong> attached. Then create a new script called <strong>ColourChange</strong>&nbsp;and add the following code:</p>



<pre class="wp-block-preformatted"><strong>&nbsp;   SpriteRenderer sprite;</strong>
   
<strong>    void Start()</strong>
<strong>    {</strong>
<strong>        sprite = GetComponent&lt;SpriteRenderer&gt;();</strong>
<strong>    }</strong>
    
<strong>    void Update()</strong>
<strong>    {</strong>
<strong>        if(Input.GetKeyDown(KeyCode.C))</strong>
<strong>        {            </strong>
<span style="color:#339966;"><strong>            // Change the 'color' property of the 'Sprite Renderer'</strong></span>
<strong>            sprite.color = new Color (1, 0, 0, 1); </strong>
<strong>        }</strong>
<strong>    }</strong></pre>



<p>The very first thing we do is create a variable to store the&nbsp;<strong>Sprite Renderer</strong>, and we call this&nbsp;<strong>sprite.</strong> Then in&nbsp;<strong>Start()</strong> we get the component for the&nbsp;<strong>Sprite Renderer</strong>, so this way we can access it&#8217;s&nbsp;<strong>color</strong> property. If your sprite is in a child game object of where you attached this script, instead of&nbsp;<strong>GetComponent</strong>, use&nbsp;<a href="https://stuartspixelgames.com/2017/08/02/make-all-of-objects-children-into-an-array-unity-c/"><strong>GetComponentInChildren</strong></a> like this:</p>



<pre class="wp-block-preformatted"><strong>sprite = GetComponentInChildren&lt;SpriteRenderer&gt;();</strong></pre>



<p>Now in&nbsp;<strong>Update()</strong> we first of all check to see if the key&nbsp;<strong>C</strong> is being pressed on the keyboard. If you&#8217;re confused with how that works, read the Unity documentation on it <a href="https://docs.unity3d.com/ScriptReference/Input.GetKeyDown.html">here</a>; it&#8217;s helpful.</p>



<p>Then we change the&nbsp;<strong>color</strong> property of our&nbsp;<strong>Sprite Renderer</strong> by creating a new&nbsp;<strong>Color</strong>. This literally just means we&#8217;re changing those same colour properties from earlier, but with code. It looks like this:</p>



<pre class="wp-block-preformatted"><strong>sprite.color = new Color (Red, Green, Blue, Alpha);</strong></pre>



<p>Confusingly, instead of being a value between 0 and 255, like it was in the editors colour changer, now we have to give it a value between 0 and 1 for each colour property, 0 being no colour, 1 being full colour for that property.</p>



<p>So in other words, if you run your game and test it now, it will tint your sprite red, because we told it to be:</p>



<ul><li>Full Red</li><li>No Green</li><li>No Blue</li><li>Fully Visible</li></ul>



<p>Likewise if we changed our colour property like this:</p>



<pre class="wp-block-preformatted"><strong>sprite.color = new Color (0, 1, 0, 1);</strong></pre>



<p>It would tint our sprite green. Try it.</p>



<p>Then if you did this:</p>



<p><strong>sprite.color = new Color (1, 1, 1, 0);</strong></p>



<p>Your sprite would be its normal colour, but it would be transparent, because we turned the alpha all of the way down. So that&#8217;s a nice way to make your sprite invisible, but still enabled.</p>



<p>You should be able to use this to create some nice effects in your games now like a character flashing when it&#8217;s hurt.</p>



<div class="is-layout-flow wp-block-group"><div class="wp-block-group__inner-container">
<p class="has-text-align-center has-medium-font-size"><strong>If you enjoyed reading, try my game <a href="https://www.humblebundle.com/g/puzzledorf">Puzzledorf</a>.</strong></p>
</div></div>


<div class="wp-block-image">
<figure class="aligncenter size-large"><a href="https://www.humblebundle.com/g/puzzledorf"><img data-attachment-id="7638" data-permalink="https://stuartspixelgames.com/wide-ad/" data-orig-file="https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png" data-orig-size="1920,586" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="wide-ad" data-image-description="" data-image-caption="" data-medium-file="https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=300" data-large-file="https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=1024" src="https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=736" alt="" class="wp-image-7638" srcset="https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=736 736w, https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=1472 1472w, https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=150 150w, https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=300 300w, https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=768 768w, https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=1024 1024w" sizes="(max-width: 736px) 100vw, 736px" /></a></figure></div>]]></html><thumbnail_url><![CDATA[https://stuartspixelgames.files.wordpress.com/2017/03/c.png?fit=440%2C330]]></thumbnail_url><thumbnail_width><![CDATA[415]]></thumbnail_width><thumbnail_height><![CDATA[255]]></thumbnail_height></oembed>