Save color setting?

I’m new to Unity (getting the hang of it very quickly though) and somewhat new to scripting. I’m trying to get GUI sliders in an options screen to change the color of a texture, which I have working, and then automatically save that setting to load in the game or the next time the options screen is loaded, which isn’t working. I get this error:

Assets/Objects/Rings/Scripts/ChangeRingMatColor.js(20) error BCE0017: The best overload for the method ‘UnityEngine.PlayerPrefs.SetString(String, String)’ is not compatible with the argument list ‘(String, UnityEngine.Color)’.

It doesn’t matter if I use String, Int, or Float, I get the same error with it’s respective type.

Here’s the code:

var ringmatColor : Color;

function OnGUI () {
	ringmatColor = RGBSlider (Rect (10,10,200,10), ringmatColor);
}

function RGBSlider (screenRect : Rect, rgb : Color) : Color {
	rgb.r = GUI.HorizontalSlider (screenRect, rgb.r, 0.0, 1.0);
	screenRect.y += 20; // <- Move the next control down a bit to avoid overlapping
	rgb.g = GUI.HorizontalSlider (screenRect, rgb.g, 0.0, 1.0);
	screenRect.y += 20; // <- Move the next control down a bit to avoid overlapping
	rgb.b = GUI.HorizontalSlider (screenRect, rgb.b, 0.0, 1.0);
	return rgb;
}
function Update () {
	renderer.material.SetColor("_Color", ringmatColor);
	
PlayerPrefs.SetString("Mat Color", ringmatColor);
}

And I don’t have the loading part coded yet as you can see. Can anyone point out what’s going wrong?

The problem is that when you pass the color object, it’s not always smart enough to know that you want it converted to a string. You can use .ToString() on the color to force it to be passed as a string. However, if I recall correctly, Color.ToString() only outputs one digit of the color: i.e. (0.5, 0.8, 0.3). This could cause precision problems when you read it back in. So you’re probably better off either using SetFloat, or constructing your own string from the .r, .g, .b values.

Or you could use functions like:

//wrapper function for multiple SetFloat's for a color
static function SetColor(name : String, color : Color) {
PlayerPrefs.SetFloat(name+".r", color.r);
PlayerPrefs.SetFloat(name+".g", color.g);
PlayerPrefs.SetFloat(name+".b", color.b);
PlayerPrefs.SetFloat(name+".a", color.a); //if needed
}

static function GetColor(name : String, defaultValue : Color) : Color {
var col : Color = Color(0,0,0,0);
col.r = PlayerPrefs.GetFloat(name+".r", defaultValue.r);
col.g = PlayerPrefs.GetFloat(name+".g", defaultValue.g);
col.b = PlayerPrefs.GetFloat(name+".b", defaultValue.b);
col.a = PlayerPrefs.GetFloat(name+".a", defaultValue.a); //if needed
return col;
}

It seems like what you wrote should work great, but I’m not understanding how to implement it.

For the menu, I need it to set the RBG sliders to what the previous values were automatically when the scene loads. I need to save the RBG value, but it is essentially also saving the position of the GUI sliders.

For in-game use, it just needs to set the color value of the texture with the value set with the sliders, which I’m pretty sure I know how to do.

Thanks for what you already have done for me.

You could try to save the slider position, instead of color RGB values. That way, the sliders will be at the correct position upon load and the RGB values will adjust themselves accordingly.