convert string to color


Is there a way to convert string to color.

I have a string veriable colorName, and I assigne it value such as “yellow”. I want to use this value as a color.

Smt like that:
Not material.color=color.yellow

Thanx for any help,

This is an old post, but not satisfactorily answered.

In C# I created an extension class that converts the name to Color

public static class ColorExtensions
    /// <summary>
    /// Convert string to Color (if defined as a static property of Color)
    /// </summary>
    /// <param name="color"></param>
    /// <returns></returns>
    public static Color ToColor(this string color)
        return (Color)typeof(Color).GetProperty(color.ToLowerInvariant()).GetValue(null, null);

You’d use it like

Color yellow = "yellow'.ToColor();

Use “ColorUtility.TryParseHtmlString” to convert a color string: Unity - Scripting API: ColorUtility.TryParseHtmlString

Color MyColour = Color.clear;

ColorUtility.TryParseHtmlString (“yellow”, out MyColour);

Enum.Parse Method (System) | Microsoft Learn has an example. It supplies the Color enum, you don’t need to.