Change material during runtime c#

Hi ive spent the entire day looking for a solution but can’t find anything.

I have a chair (single mesh), with 3 materials on it, assigned to the 3 sub mesh components, as exported from max.

Now i want to change the color of lets say the white plastic from an interface palette, much like UE4arch.com - Loft complete project - YouTube.

I started out basic with:
if (Input.GetKey (KeyCode.A))
gameObject.GetComponent().material.color = Color.red

But this chooses a random material and changes that to red, so i cant specify which one relates to which
From reading around, I found an array can possibly do this (the material change part, I know the palette GUI is a separate task), but I have no idea how to construct the full this script (artist not coder)

So far i found:

Material mats;
mats = renderer.materials;
mats[1] = theNewMaterial
renderer.materials = mats;

But im not sure how to put this in the script correctly.

using UnityEngine;
using System.Collections;

public class ChangeColor : MonoBehaviour
{

public Material[] mats;  

void Update () 

{
	mats = GetComponent<Renderer>().materials;
	mats[1] = theNewMaterial; 
	GetComponent<Renderer>().materials = mats;

}

}

As much as I know about C# at the moment, thus a bit lost as I get error CS0103: The name `theNewMaterial’ does not exist in the current context and not sure how to ref that properly

Then I would also be looking at how to do the swapping via the interface palette as a second objective :slight_smile:

Thanks

The approach is correct, but theNewMaterial is an undefined variable. You should initialize it as an exposed variable, as you did with mats.

public Material theNewMaterial;

Then, through the Inspector, drag and drop a Material from your project to the variable’s value.

EDIT:

You can have three exposed variables identifing materials:

public Material plastic;
public Material wood;
pubblic Material metal;

Then, when you have to assign them at the chair:

 mats = GetComponent<Renderer>().materials;
 //Here, you have to assign with the exact order!
 mats[0] = plastic; 
 mats[1] = wood;
 mats[2] = metal;
 GetComponent<Renderer>().materials = mats;

Assign the materials in array as u did and get the materials present in the object n assign to it.

count = 0;
foreach(Renderer r in GetComponentsInChildren<Renderer>())
{
    r.material=mats[count];
    count+=1;
}

Probably the best bet will be to raycast at the chair or create buttons that completely envelop the interchangeable parts. Then on a button press or a ray hit you would just set the current material to be the next one in the list, or if you are at the end of the list loop back around to the beginning again.

I can post something of an example of it is needed, but I take no responsibility for inefficient or unoptimized code.

Hi all,

Is this chat still live, i have a similar question to finish off my project.

Your help would be most appriciated.

Thanks and hope to hear from you soon.

Curtis

You cannot change the materials one by one, you have to replace the entire material[] array:

    public Renderer r;
    public Material newMaterial;

void changeMat()
{
//this code creates a copy of the original material array, replaces the first element with the 
//"newMaterial" then assigns the new amterial list to the renderer
            Material[] newMats = new Material[r.materials.Length];
            r.materials.CopyTo(test, 0);
            newMats[0] = newMaterial;
            r.materials = newMats;
}

//I use this code to replace my character’s materials when it dies, it should work