Address
304 North Cardinal St.
Dorchester Center, MA 02124

Work Hours
Monday to Friday: 7AM - 7PM
Weekend: 10AM - 5PM

Unity how to change color of object

Unity how to change color of object

There are 2 ways to change the color of an object on Unity: via the inspector or via the code:
Here, we’ll look only at the second method:

Summary:

Change the color of spriteRenderer

To change the color of a spriteRenderer, utilize the ‘color‘ property.

using UnityEngine;

public class SpriteRendererModifier : MonoBehaviour
{
    public SpriteRenderer spriteRenderer;

    private void Start()
    {
        spriteRenderer.color = Color.white;
    }
}

Change the color of 3D object (MeshRenderer)

To change the color of a 3D object, utilize the “SetColor” method of its material with “_Color” parameter.

using UnityEngine;

public class MeshRendererModifier : MonoBehaviour
{
    public MeshRenderer meshRenderer;

    private void Start()
    {
        meshRenderer.material.SetColor("_Color", Color.red);
    }
}

Image

To change the color of an Image, utilize the ‘color‘ property.

using UnityEngine;
using UnityEngine.UI;

public class ImageModifier : MonoBehaviour
{
    public Image image;

    private void Start()
    {
        image.color = Color.red;
    }
}

RawImage

To change the color of an RawImage, utilize the ‘color‘ property.

using UnityEngine;
using UnityEngine.UI;

public class RawImageModifier : MonoBehaviour
{
    public RawImage rawImage;

    private void Start()
    {
        rawImage.color = Color.red;
    }
}

Text UI (TextMeshPro)

To change the color of a text in UI, utilize the ‘color‘ property.

using UnityEngine;
using TMPro;

public class TextMeshProUGUIModifier : MonoBehaviour
{
    public TextMeshProUGUI textMeshProUGUI;

    private void Start()
    {
        textMeshProUGUI.color = Color.red;
    }
}

Text 3D (TextMeshPro)

Same as UI, to change the color of a text in 3d world, utilize the ‘color‘ property.

using UnityEngine;
using TMPro;

public class TextMeshProModifier : MonoBehaviour
{
    public TextMeshPro textMeshPro;

    private void Start()
    {
        textMeshPro.color = Color.red;
    }
}

Button

To change the color of a button, utilize the ‘colors’ property.
This property allows each state in the button to be modified independently.

using UnityEngine;
using UnityEngine.UI;

public class ButtonModifier : MonoBehaviour
{
    public Button button;

    private void Start()
    {
        var colors = button.colors;

        colors.normalColor = Color.red;
        colors.highlightedColor = Color.red;
        colors.pressedColor = Color.red;
        colors.selectedColor = Color.red;
        colors.disabledColor = Color.red;

        button.colors = colors;
    }
}

A different technique is used to change the color of a button in the Inspector.

Particle (ParticleSystem)

To change the color for particles utilize the ‘main‘ property. However it’s a little different: in this component there are 3 modifiable colors, the start color, the color through time, and the color by speed.
Each of these colors uses a specific gradient, the MinMaxGradient, which can be used to display simple colors or gradients.

using UnityEngine;
using static UnityEngine.ParticleSystem;

public class ParticleSystemModifier : MonoBehaviour
{
    public ParticleSystem particleSystemRef;

    public MinMaxGradient minMaxGradient;

    private void Start()
    {
        // Start color
        var main = particleSystemRef.main;
        main.startColor = minMaxGradient;

        // Color over lifetime
        var colorOverLifeTime = particleSystemRef.colorOverLifetime;
        colorOverLifeTime.color = minMaxGradient;

        // Color by speed
        var colorBySpeed = particleSystemRef.colorBySpeed;
        colorBySpeed.color = minMaxGradient;
    }
}

Particle with Trail

To change the color of a trail with particle, utilize the ‘trails‘ property. Like particles, the MinMaxGradient will be used for set colors.

using UnityEngine;
using static UnityEngine.ParticleSystem;

public class TrailParticleSystemModifier : MonoBehaviour
{
    public ParticleSystem particleSystemRef;

    public MinMaxGradient minMaxGradient;

    private void Start()
    {
        var trails = particleSystemRef.trails;

        trails.colorOverLifetime = minMaxGradient;
        trails.colorOverTrail = minMaxGradient;
    }
}

Trail (TrailRenderer)

To change the color of a trail, utilize the ‘colorGradient‘ property.

using UnityEngine;

public class TrailRendererModifier : MonoBehaviour
{
    public TrailRenderer trailRenderer;

    public Gradient gradient;

    private void Start()
    {
        trailRenderer.colorGradient = gradient;
    }
}

Line (LineRenderer)

To change the color of a line, utilize the ‘colorGradient‘ property

using UnityEngine;

public class LineRendererModifier : MonoBehaviour
{
    public LineRenderer lineRenderer;

    public Gradient gradient;

    private void Start()
    {
        lineRenderer.colorGradient = gradient;
    }
}

I hope this has helped you, now you know how to change the color of object in Unity.

Illustration by Magic Creative of Pixabay

Have you just started your Unity adventure?
I recommend this article to get you started in the best possible way: The Best Advice I Wish I Had When I Started Game Development

Check our other articles about Unity:

Leave a Reply

Your email address will not be published. Required fields are marked *