- Home /
Swapping out a single material on a SkinnedMeshRenderer at runtime
I have a GameObject that, when clicked, needs to change the Material assigned to another GameObject. I'm using this to swap out the costumes of my character when they click on that costume.
public class CostumeSelect : MonoBehaviour {
public Material costume;
public string character;
public int number;
public GameObject model;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void OnMouseDown () {
Material[] temp = model.transform.GetChild(0).renderer.materials;
temp[0] = costume;
model.transform.GetChild(0).renderer.materials = temp;
}
}
When they click on this GameObject, it's supposed to take the Material assigned in the inspector (var costume) and assign it to the material of the other GameObject (var model). I've tried this too, to no avail:
public class CostumeSelect : MonoBehaviour {
public Material costume;
public GameObject model;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void OnMouseDown () {
model.transform.GetChild(0).renderer.material = costume;
}
}
The only reason I'm trying the first option is because someone said there's a quirk in Unity where you can't just change a material, you have to swap out the whole array. The thing that's driving me up the wall is that, when debugging, both costume and renderer.material show the correct Material name when inspected. Are these Materials not being loaded for some reason? How can I determine if they're not being loaded? What am I doing wrong here?