Address
304 North Cardinal St.
Dorchester Center, MA 02124
Work Hours
Monday to Friday: 7AM - 7PM
Weekend: 10AM - 5PM
We use cookies to ensure that we provide you with the best possible experience on our site.
Your essential source on Unity and web development
Your essential source on Unity and web development
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:
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;
}
}
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);
}
}
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;
}
}
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;
}
}
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;
}
}
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;
}
}
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.
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;
}
}
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;
}
}
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;
}
}
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