- Home /
Unable to set second material programmatically
Hi,
I need to set a second material to a gameobject programmatically.
But when I run...
rendering.materials[1] = selectedMat;
... selectedMat is never applied to materials[1].
If I run again with the following debugs...
Debug.Log("Expected: "+selectedMat.name);
Debug.Log("Actual: "+rendering.materials[1]);
...I get the following:
Expected: OutlinedMaterial_Friendly
Actual: Default-Material (Instance) (UnityEngine.Material)
So Unity is failing to assign the requested material. Its somehow loading a completely different "default" material. I am able to assign the OutlinedMaterial_Friendly to Material slot 2 in the inspector, however, this is insufficient for my purposes. I need to be able to change this slot programmatically.
Any ideas on what I am doing incorrectly? Or is this a bug in Unity? Thanks
Answer by DSebJ · Feb 14, 2017 at 01:12 AM
You can't update the array of materials directly. You need to take a copy of it, update the position you want to change and then update the whole array of materials. I.e.:
Material[] matArray = mesh.materials;
matArray[1] = mat;
mesh.materials = matArray;
Note: This looks like the expected behaviour, from the manual: "Note that like all arrays returned by Unity, this returns a copy of materials array. If you want to change some materials in it, get the value, change an entry and set materials back." - https://docs.unity3d.com/ScriptReference/Renderer-materials.html
You are amazing and I am a bit ashamed that I glossed over that note. Thanks for setting me right
No problems, I was curious when I saw your post and thought it looked like it should work that way too :-)
If you want to change all the materials to your material:
if (renderer.materials.Length > 1) { for (int i = 0; i < matArray.Length; i++) { matArray[i] = my$$anonymous$$aterial; renderer.materials = matArray; } }
Also Dseb you saved me so much ttime tysm.
It works as coded above .. but I'm trying to do this in the editor (not at runtime).. which creates an INSTANCE of unchanged array elements.
ie: I change material element 1 (works fine), but the editor creates an INSTANCE of material element 0 ... there's a bit fat (Instance ) beside material array element 0 -- how can we revert that?
UPDATE used sharedMaterials to fix this instead of materials.. hope it helps others
Answer by mefirstgames42 · Nov 14, 2020 at 09:08 PM
Hi there,
Try something like
var mats = renderer.sharedMaterials;
mats[materialIndex] = material;
renderer.sharedMaterials = mats;
Answer by aditya · Feb 13, 2017 at 05:02 AM
you can set more than one materials from scripts only if you have more than one slots of material in inspector, you had to manually describe how many materials you want on your model (they should be equal to the number of UV sets your model has) .... Go to INSPECTOR > MESH RENDERER >MATERIALS and increase SIZE field
Hi Aditya,
Thank you for the response, but this is not the problem. I already have 2 slots for materials. If I didn't, the above code would have given an index out of range exception.
Remember that I am able to apply both textures in the inspector, so the problem is clearly not the way that the GameObject is organized. The problem must be in the script or in Unity itself.
Answer by GoblinGameWorks · Apr 07 at 04:56 AM
I know the thread is a bit old but in case others come across this issue in 2022 +
@DSebJ 's answer above got me to my solution. I expect most people looking for this are trying to add an outline material to a character model (or similar 3D model).
you must have at least 2 materials on a mesh renderer or skinned mesh renderer component.
this script achieves replacing the mesh renderer's materials array when the mouse hovers over it. attach this script to the parent object of the 3d model in question.
Drag the 2 materials into the array slots in the inspector ( the ones that will replace the original ones when you hover over the object)
remember you are replacing the whole array not just 1 material.
-------------------------------------------
public class AddMaterial : MonoBehaviour { public Material[] newMaterials; Material[] originalMaterials; Renderer rend; private void Start() { rend = transform.GetChild(0).GetComponent<SkinnedMeshRenderer>(); originalMaterials = rend.materials; } private void OnMouseOver() { rend.materials = newMaterials; } private void OnMouseExit() { rend.materials = originalMaterials; }
}
Your answer
Follow this Question
Related Questions
How to apply a color to the second texture in this shader? 1 Answer
Mobile Material Optimization 0 Answers
Materials are not visible on some android phones? 0 Answers
How select material from inspector? 0 Answers
Changing material during runtime 1 Answer