I am trying to make this color picker script work

Hello. I found some code on the Unity forums, from the post from GaborD here

The code is:

if (GUI.RepeatButton (Rect (xxx,yyy,215,55), colorPicker)) {
            pickpos = Event.current.mousePosition;
            aaa = pickpos.x-xxx-9;
            bbb = pickpos.y-yyy-5;
            col = colorPicker.GetPixel(aaa,41-bbb);

            model.materials[matnum].SetColor("_Color",col);
        }

I have gotten these errors:

Unknown identifier: 'xxx'

I added var xxx; to the top, then got

Unknown identifier: 'yyy'

I added var yyy; to the top, then got

Unknown identifier: 'colorPicker'

I added var colorPicker; to the top, then got

Unknown identifier: 'model'

I added var model; to the top, then got

Unknown identifier: 'colorPicker'

I added var colorPicker; to the top, then got

model.materials[matnum].SetColor("_Color",col);

I added var model : Transform; to the top, then got

So I am assuming this is where you drop the model or prefab I want to control the color of. But how do I do this right? Am I doing any of this right? Should I assign anything to those variables, `xxx`, `yyy`, and `colorPicker`? Thank you!

Here's the script with the correct variable declarations.

// position on x axis
var xxx : float = 10;
// position on y axis
var yyy : float = 10;
// assign your colorpicker texture to this variable
var colorPicker : Texture2D;
// assign your model to this variable
var model : Renderer;
// The material index of the material you want to pick a color for.
var matnum : int = 0;
function OnGUI() {    
    if (GUI.RepeatButton (Rect (xxx,yyy,215,55), colorPicker)) {
        var pickpos : Vector2 = Event.current.mousePosition;
        var pixelPosX : int = pickpos.x-xxx-9;
        var pixelPosY : int = 41-(pickpos.y-yyy-5);
        var col : Color = colorPicker.GetPixel(pixelPosX,pixelPosY);
        model.materials[matnum].SetColor("_Color",col);
    }
}

And as GaborD mentioned you may have to adjust the offset values depending on your texture: (215; 55; -9;-5;41;)

there is no need to use “magic numbers” and trial and error here if you use GetPixelBilinear function instead of GetPixel:

    if (GUI.RepeatButton(new Rect(beginX, beginY, dimX, dimY), colorSpectrumTexture, GUI.skin.GetStyle("ColorPickerStyle"))) {
        Vector2 pickpos = Event.current.mousePosition;
        Vector2 coords = new Vector2((pickpos.x - beginX) / dimX, -(pickpos.y - beginY) / dimY);
        Color color = colorSpectrum.GetPixelBilinear(coords.x, coords.y);
    }

Note: Make sure your “ColorPickerStyle” has no padding, margin and border; has UpperLeft alignment; and dimX and dimY are proportional to the size of your texture, i.e. it fills the entire Rect - for some reason I think Unity can’t correctly stretch a background image of a GUI controller, like a RepeatButton, even with StretchWidth and StretchHeight enabled).