Changing Color using C# script in Unity

One of the key components of any game is its color palette, which sets the mood, tone and overall aesthetic of the game. The Unity engine provides a rich set of tools and functions for working with colors in a game, which include the Unity Color class. In this article we will explore the various aspects of the Unity Color class, and see how to change the color of various objects using C# script in Unity.

Unity color banner

Unity Color Class

Color class in Unity takes in 4 inputs. The first 3 are the red, green and blue values and the 4th input is the alpha value.

Color(0.5f, 0.2f, 0.1f, 0.8f);

The color class also takes in pre-defined color names shown in the table below. Now let’s see how to define a new custom color in Unity.

Defining new Color in Unity

Color newColor = new Color(0.5f, 0.2f, 0.1f, 0.8f);

Unity color class has a few pre-defined color names that you can use with the color class

Setting Color with Color Names

Color nameRGBA VAlue
red1, 0, 0, 1
green0, 1, 0, 1
blue0, 0, 1, 1
black0, 0, 0, 1
white1, 1, 1, 1
grey0.5, 0.5, 0.5, 1
gray0.5, 0.5, 0.5, 1
cyan0, 1, 1, 1
clear0, 0, 0, 0
magenta1, 0, 1, 1
yellow1, 0.92, 0.016, 1

You can use these names to set the Color value as shown below

Color newColor = new Color.black;

Changing Color of a material

In most cases, Color is set to the material of the object. The material in turn is part of the renderer component. So, to set the color of a game object, you need to get the renderer component and set the color of the material.

Let’s try to set the color of the default cube in Unity

  1. Add a 3D object>Cube to the scene.
  2. Create and add a script called ColorChange to the cube.
  3. Copy and paste the code below to the script.
  4. Play the game and the cube should change color to black
using UnityEngine;

public class ColorChange : MonoBehaviour
{
    Renderer lr;
    // Start is called before the first frame update
    void Start()
    {
        lr=GetComponent<Renderer>();
        lr.material.color=Color.black;
        
    }

}

If you want to use RGB then use this code

using UnityEngine;

public class ColorChange : MonoBehaviour
{
    Renderer lr;
    // Start is called before the first frame update
    void Start()
    {
        lr=GetComponent<Renderer>();
        Color col=new Color(0, 0, 0, 1);
        lr.material.color=col;
    }

}

Changing Color of a 2D sprite

2D sprites are little different than other objects. The color property is part of the renderer component. So, you can set it directly from the renderer.

To set the color of the sprite follow the steps below

  1. Add a new script called ColorChange to your 2D sprite.
  2. Add the code below to your sprite.
  3. Play the game and the color should change to black.
using UnityEngine;

public class ColorChange : MonoBehaviour
{
    SpriteRenderer spr;
    // Start is called before the first frame update
    void Start()
    {
        spr=GetComponent<SpriteRenderer>();
        spr.color=Color.black;
    }

}

Adjusting Alpha of Unity Color

Unity allows you to control the red, green, blue and alpha value. You can access the individual values using r,g,b and a parameters of Unity class.

Let’s try to change the alpha value of the default cube to 0.5 using script.

using UnityEngine;

public class ColorAlpha : MonoBehaviour
{
    Renderer lr;
    Color col=Color.black;
    // Start is called before the first frame update
    void Start()
    {
        lr=GetComponent<Renderer>();
        col.a=0.5f;
        lr.material.color=col;
    }

}

Slowly changing color using lerp

If you are new to lerp then read our blog post on Unity lerp for more details. Now let’s try to change from one color to another using the lerp function.

Color change from black to white in 5 seconds
using UnityEngine;

public class Unity_lerp : MonoBehaviour
{
    Renderer col_rend;
    float timer=0;
    void Start()
    {
        col_rend=GetComponent<Renderer>();
        
    }

    // Update is called once per frame
    void Update()
    {
        col_rend.material.color= Color.Lerp(Color.black, Color.white,timer/5);
        timer+=Time.deltaTime;
    }
}

Random color in Unity

To generate random colors, you just need to generate 3 random numbers between 0 and 1 and assign it to the r,g,b value of the color class. Here is the sample code to generate random color after 3 seconds delay.

using UnityEngine;
using System.Threading.Tasks;

public class ColorChange : MonoBehaviour
{
    Renderer lr;
    Color col;
    // Start is called before the first frame update
    async void Start()
    {
        lr=GetComponent<Renderer>();
        await Task.Delay(3000);
        col.r=Random.Range(0f,1f);
        col.g=Random.Range(0f,1f);
        col.b=Random.Range(0f,1f);
        lr.material.color=col;
    }

}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from VionixStudio

Subscribe now to keep reading and get access to the full archive.

Continue reading