Script-Assigned Vehicle Colors

Using HDRP in a 3D project.
Eventually I want to let my players pick the color of their vehicle.
I am using a solid-color material for my prefab.
I want to have many instances of my prefab in my scene, but I want them all to have different colors.
But I find that when I change the colors on one instance, they all change colors.

What I’d like is to be able to read the color (all other material properties stay the same) from a script property and have the object change it’s colors without changing all instances of the vehicle.

ALSO: the vehicle has many parts and some of the parts have multiple materials (metal, chrome, glass, etc.)

So I need to pick out all the items using the “Metal” shader, and change just the color property.

I can’t even seem to find a way to find the materials in my instance, much less change one property.

You have two options when getting the material from a renderer, just material or shared material. Shared is the material the object shares and if you just get the material, it will turn it into an instanced material that you can edit. Like so;

    private void Start()
    {
        var renderer = GetComponent<Renderer>();

        var mat = renderer.material;

        mat.color = Color.blue;

        renderer.material = mat;
    }

Here an example script, simply assign your prefab instances and the colors you want:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ChangeColors : MonoBehaviour
{
    [Tooltip("Assign your prefab instances here")]
    public List<Transform> transforms;

    [Tooltip("Assign your colors here")]
    public List<Color> colors;

    // Start is called before the first frame update
    void Start()
    {
        int i = 0;
        foreach (Transform transform in transforms)
        {
            Color color = colors[i++];
            Renderer[] renderers = transform.gameObject.GetComponentsInChildren<Renderer>();
            foreach (Renderer renderer in renderers)
            {
                renderer.material.color = color;
            }
        }
    }
}

Thanks, this looks like basically what I want to do except:

  1. my “this” instance has 10 parts
  2. many of the parts have many materials.
    see the images attached.
    6066038--657203--Materials1.jpg

    …how with your code would I change only the color of the Metal-Doublesided in the cowl and other specific colors?
    your code seems to only pick the first color associated with the first object (I think).

Many parts is not a problem, but if your meshes have more than one material here’s the change:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ChangeColors : MonoBehaviour
{
    [Tooltip("Assign your prefab instances here")]
    public List<Transform> transforms;

    [Tooltip("Assign your colors here")]
    public List<Color> colors;

    // Start is called before the first frame update
    void Start()
    {
        int i = 0;
        foreach (Transform transform in transforms)
        {
            Color color = colors[i++];
            Renderer[] renderers = transform.gameObject.GetComponentsInChildren<Renderer>();
            foreach (Renderer renderer in renderers)
            {
                foreach (Material material in renderer.materials)
                {
                    material.color = color;
                }
            }
        }
    }
}