Parsing a String to a Color

How come this works:

var idk1 : String = "123";
var idk2 : int = int.Parse(idk1);
print(idk2);

But this doesn’t:

var idk1 : String = "(1.0, 1.0, 1.0, 1.0)";
var idk2 : Color = Color.Parse(idk1);
print(idk2);

I get this error: MissingMethodException: Method not found: ‘UnityEngine.Color.Parse’.

Because there isn’t a Color.Parse() method. You can’t just make up methods. You need to check the API to see if they exist. int.Parse() is a real method in the .Net library. It wouldn’t be very hard to write a parser for a string though:

public static class ColorExtensions {
	public static function ParseColor (col : String) : Color {
		//Takes strings formatted with numbers and no spaces before or after the commas:
        // "1.0,1.0,.35,1.0"
		var strings = col.Split(","[0] );

		var output : Color;
		for (var i = 0; i < 4; i++) {
			 output _= System.Single.Parse(strings*);*_

* }*
* return output;*
* }*
}
That should work. It will give you a warning saying that declaring a static function in a static class is redundant, but I prefer to leave it in for clarities sake. It isn’t a harmful warning.
Example:
var col = ColorExtensions.ParseColor( “0.0,1.0,.75,.2” );
Debug.Log(col);

Convert from string(colorInStr) to Color
colorInStr = “RGBA(0.471, 0.024, 0.024, 1.000)”

using System.Text.RegularExpressions;
private Color StrParseToColor(string colorInStr){
Debug.Log("color ".StrColored(DebugColors.green) + colorInStr);

                string[] btwSpaces = colorInStr.Split(' ');
                float[] colorValues = new float[btwSpaces.Length];
    
                Regex regex = new Regex("[0-9]+[.[0-9]+]?");
                for (int i = 0; i < btwSpaces.Length; i++)
                {
                    Match m = regex.Match(btwSpaces*);*

if (m.Success)
{
float value = float.Parse(m.Value);
colorValues = value;
Debug.Log("ME.Value: " + value);
}
else Debug.Log(“Not success”);
}

Color colorReturn = new Color();
colorReturn.r = colorValues[0];
colorReturn.g = colorValues[1];
colorReturn.b = colorValues[2];
colorReturn.a = colorValues[3];

return colorReturn;
enter code here }