Script to allow the player to change material colour in game?

I’m looking for a script or any other way to allow the player to change the colour of a specific material in game. Basically, something like a basic preset colour chooser or even a colour wheel in the UI to let the player change the colour of the character’s body material. Is this possible?

Changing the color in code is simple enough, but incorporating that into UI isn’t, because Unity, as far as I know, doesn’t have a pre-made color wheel, so you’ll have to do some work on that.


As far as code goes, I can help. If you want to alter a material of a singular object, then you can just modify the material’s color, no problem, like this:

GetComponent<MeshRenderer>().material.color = randomColor;

However, if you want to modify a material, which multiple objects share, for a specific object you have to make a copy of the assigned material, essentially treating it as a template material and alter that, like this:

// The material assigned to the object
Material tempalteMaterial = GetComponent<MeshRenderer>().material;

// Making a new material based on the assigned one
Material objectSpecificMaterial = new Material(tempalteMaterial);
GetComponent<MeshRenderer>().material = objectSpecificMaterial;

// Then you can simply modify the material's color and it'll only affect a single object
GetComponent<MeshRenderer>().material.color = randomColor;

When I say that incorporating the above code into UI isn’t simple, I mean it’s not simple to do so well. I’ve only ever used sliders to do this (each would be for the RGB values of the material’s color). But, I’m certain some research will give you results.

@strangecrackcartoons
Maybe this could help.

    private Renderer yourRenderer;

    public void WhateverYouWantToDo() 
    {
            yourRenderer.material.SetColor("_Color", Color.red); //This Color is just an example
            //You can also make a color variable and set it to that and assign the color in Inspector
    }

I hope this would help =D