Unity how to change color of object

Unity how to change color of object

On Unity, there are two ways to change the color: via the inspector window or via the code:
Here, we’ll look only at the second method:

Summary:

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;
    }
}

3D object (MeshRenderer)

To change the color of a 3D object, utilize the “SetColor” method of its material with the “_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 a 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 the 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 a 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 in the UI, 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;
    }
}

It’s a different technique to change the color of a button in the Inspector.

Particle (ParticleSystem)

To change the color of particles, utilize the ‘main‘ property. However, it’s a little different in this component. Instead of one, there are three colors: the start color, the color through time, and the color by speed.
Each color uses a specific gradient, the MinMaxGradient, which displays 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 a particle, utilize the ‘trails‘ property.
Like particles, the MinMaxGradient 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.
Changing the color of an object in Unity no longer holds any secrets for you.

Illustration by Magic Creative of Pixabay

Leave a Reply

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