Multi MatID objec - Material Change

Hi,

I am having difficulty figuring out how to take a gameobject with multiple material ID’s and accessing them in script so I could switch between, or change the color of, what material is present for that given ID.

So if I had a cube split into thirds with 3 material ID’s I would like, for example, to have material 1ID when function onmousedown is done to switch the Material 1ID to a different material.

Hopefully I am explaining this right.

Any insight would be greatly appreciated.

Thanks.

Do you mean you want the cube to be 3 different colors at once… Or you just want it to be one solid color, but able to switch between 3 different colors based on conditions in the game?

If you mean the second here is some basic code you could attach to an object:
You would need to make 3 materials as Assets and Drag them into the inpsector of this script.
ColorCube

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class ColorCube : MonoBehaviour
{
    public enum ColorStates
    {
        Normal,
        MouseDown,
        Hit
    }

    public Material[] mats;
    private Renderer rend;

    void Awake()
    {
        rend = GetComponent<Renderer>();
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
            SetColor(ColorStates.MouseDown);
        if (Input.GetMouseButtonUp(1))
            SetColor(ColorStates.Normal);
    }

    public void SetColor(ColorStates state)
    {
        rend.material = mats[(int)state];
    }
}

You could have other objects call SetColor as well say if a bullet hit your object and you had an OnCollisionTrigger you might have something like this:

void OnCollisionEnter(Collision collision) {
       ColorCube script = collision.gameObject.GetComponent<ColorCube>();
       if (script != null)
             script.SetColor(ColorStates.Hit);
}

Thanks takatok for the reply and writing out some scripts!

I am trying to have the cube have 3 different colors at once.

I have attached a pic. So I would like to have those lambert materials individually accessible in script. I cant figure out how to call them independently. Anything I try atm works on the lowest listed material only.

Any thoughts?

I’m unfamiliar with how you set it up to stripe it like that. Did you have a custom shader (probably not since your picture shows standard shader) or is there some way to tell unity to use a particular Material for each third?

One obvious solution that leaps to mind is to make an empty gameobject that has 3 children each of which is a cube whose height is 1/3rd the height of your original cube. Each cube slice can have its own material and you would attach scripts to move it to the empty gameobject (so they move as a unit) and each cube can just change its own color independent of the others. The empty GO could have that script as well.

Thanks for the quick responses takatok.

This cube is actually an import from a modeling software. I am able to segment the materials by having them assigned to polygons in the modeling software. So this cube is actually 28 tris rather than the usual 12.

Your workaround idea is something I have been thinking about doing, however this does create two problems for me.
This will destroy smoothing groups and I am worried about performance.