- Home /
Multiple materials on one mesh
I have unity5 and i'm trying to alter, through script multiple materials that are on the same mesh.
i am currently using the following style of code -
GameItemObjectThingy.GetComponent<Renderer> ().sharedMaterials[0].CopyPropertiesFromMaterial(materialForGO);
GameItemObjectThingy.GetComponent<Renderer> ().sharedMaterials[1].CopyPropertiesFromMaterial(secondMatForGO);
it works! which is great, but it only copies, it does not use the same material, and so batching is out :(
What i am looking to do is the same thing but using the same material so it can be batched rather than making a copy of the material.
if i do the following -
GameItemObjectThingy.GetComponent<Renderer> ().sharedMaterials[0]= materialForGO;
GameItemObjectThingy.GetComponent<Renderer> ().sharedMaterials[1]= secondMatForGO;
it does not work. anyone got any ideas?
Answer by maccabbe · May 28, 2015 at 03:38 AM
Material[] materials=GameItemObjectThingy.GetComponent<Renderer> ().sharedMaterials;
materials[0]=materialForGO;
materials[1]=secondMatForGO;
GameItemObjectThingy.GetComponent<Renderer> ().sharedMaterials=materials;
You, sir/madam are quite possibly a god! Thank you, it works fantastically!
Your answer
Follow this Question
Related Questions
How do I properly apply a transparent texture to a mesh? 2 Answers
Materials on an imported mesh 1 Answer
How to make a square mesh and remove a part of it in real time? 0 Answers
Changing all materials of selection from code 0 Answers
Scripted SetTextureOffset not working after switching to Universal Render Pipeline 1 Answer