Setting renderer.materials[] by index doesn't work?

Ran into this today, seems very strange.

This works:

renderer.material = someMaterial;

This does not:

renderer.materials[0] = someMaterial;
renderer.materials[1] = someOtherMaterial;
renderer.materials[2] = yetAnotherMaterial;

However, this does:

renderer.materials[0].mainTexture = someTexture2D;
renderer.materials[1].mainTexture = someOtherTexture2D;
renderer.materials[2].mainTexture = yetAnotherTexture2D;

At start, the renderer already has 3 materials assigned to it.

That doesn’t make a lot of sense to me.

2 Likes

What doesn’t make sense? What doesn’t work? Do you get compilation errors; runtime errors; no errors but no effect; something else?

No effect.

I had the same problem, please lookup at this thread, there is more informations.

Matt_001’s thread doesn’t help - is there any real solution to this?

edit: Ah, got it. The solution is (C#):

Material[] mats = renderer.materials;
mats[0] = someMaterial;
mats[1] = someOtherMaterial;
mats[2] = yetAnotherMaterial;
renderer.materials = mats;
13 Likes

If works (I’ll try some like this in UnitiScript) it’s a workaround. This seems clearly a bug…

Not a bug, thats expected. Same also happens with transform.rotation and transform.position for example.

in UnityScript its not required, in C# its a consequence of what it does under the hood. You must store it in a temp reference to modify it, you can’t modify the result array directly as it is not fed back in.

Hi.

I have the same problem now.
And it has no sense to me too.

Why I can’t assign this way?
renderer.materials[1] = someOtherMaterial;

(It’s a rhetoric question: If there exists a reason, it hasn’t sense for me as user…)

Confirmed: The same workaround works for UnityScript, but still seems a non sense behavior, also undocumented…

NOT REAL CODE (simplified from mine):

var newMaterial = new myComponent.SkinnedMeshRenderer.materials ;
newMaterials[0] = someMaterial;
newMaterials[1] = someOtherMaterial;
newMaterials[2] = yetAnotherMaterial;
myComponent.SkinnedMeshRenderer.materials = newMaterial;
2 Likes

Hold your horses, try this:

newMaterials[n].CopyPropertiesFromMaterial(someMaterial);

It worked for me.

Just to clarify, since this isn’t anywhere in the docs and it could be very confusing for new users trying to figure out how Unity works:

When you call renderer.material, it actually makes a copy of the material, so if you make changes to the material while the game is running, the copy is changed and not the original material in your assets folders.

If you call renderer.materials, it does the same thing, but it makes a copy of the entire materials array. So if you then assign to that array, you’re assigning to the copy, but that copy is no longer linked to the original renderer.

So, as Dreamora said, you have to know this, and if you do, you can work around it. You call renderer.materials, which makes a copy of the array, and you save that copy to a variable. You then do your changes on the copy. Finally, you assign the copy back to the renderer, with renderer.materials = myCopy (or whatever your variable is called).

Hardcore Games proposed an alternative method, but keep in mind that what he’s actually doing is copying properties from one material to another, not updating the material reference. If you have 2 different materials with 2 different shaders, his example won’t do what you think it should.

3 Likes

Actually, it’s working exactly as expected. I’m switching between two shaders, one is opaque, one is transparent, and his suggestion is working just fine.

I agree that this should be made more clear in the documentation because trying to set one material to another if it’s part of a Materials array isn’t very intuitive.

this is driving me crazy !!!

                  Material[] rendMats = tmpRend.materials;

raises an error with bla bla instanbla bla leak…

you dev guys never imagined user would like to change the material N° 17 of a renderer without fukin up everything ?

Okay… after the rumble, does anyone know the proper way to handle this thing ?

my renderer got 4 materials, and i want to change the 2nd with another material, and i just dont care about internals that make nonsense.

simply rendererMats[1] = myAwesomeMat; do not work !

And it should !

Please help !

Thanks and happy unitying !

Calm down and please stop ranting at 11-year-old posts.

Your very first stop should be the documentation for that property:

https://docs.unity3d.com/ScriptReference/Renderer-materials.html

Let me quote you the relevant section:

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.

That basically tells you EXACTLY how to do it: copy the array out, change the one you want, and copy the array back.

okay… impossible to use the materials member ( it’s then a useless class member ).
only way is to use sharedMaterials instead…

@Kurt-Dekker thanks for your answers.
I know this page and i did exactly what is said and it does NOT work !

Material[] mats = renderer.materials;

raises the error^^

Or maybe i’m stupid and do not see the obvious error there is in this code ?

This would have been good U3D shows a small example on the page you linked :confused:

Thanks anyway and happy unitying !

Since I clearly see documentation about this, wondering if this is something modified in the documentation, since you posted your issue?? Or has this documentation always been there, and you missed it?? Or maybe you didn’t understand it, since it isn’t very clear??

https://docs.unity3d.com/540/Documentation/ScriptReference/Renderer-materials.html

Doc says: “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.”

Might not be totally clear, as stated, but still documented! This is basically saying what others have said about how to properly change multiple materials on the same renderer! you can’t just directly assign new values to the array elements. But maybe your question is WHY you can’t do it this way?? Still fairly new to C#, so not sure about those details. I just know that “this is how you do it”!

1 Like

This is not the point @drakedane :slight_smile:

the doc is pretty clear about this property.

The point is that getting the property with the lil line of code i previously posted raises an error.

Just try it yourself and you’ll see it :wink:

IMHO this help page needs a simple code example for usage of this property.
However, as unity is always growing and as the doc is a huge ( at least as big as unity3D itself ) project, document team might have many other things to do^^

Happy unitying !

This caused me a lot of time spent debugging! I agree the current solution is a anti-pattern. And the engineered behavior is bug-like, requiring extra steps that are easily forgotten and make identifying problems in the code difficult.

When I set a material: renderer.materials[Idx] = newMmaterial;
there is no error but the materials are left unchanged.

In comparison if I set a single axis: transform.rotation.x = 1;
the compiler shows a error that the returned array is temporary.

Despite both being a similar issue, the prior case goes unchecked…

Materials materials array of materials renderer:

Material[] tempMaterials = ScreenMeshRenderer.materials;
tempMaterials[1] = ScreenCompletionMaterial;
ScreenMeshRenderer.materials = tempMaterials;

Material[ ] tempMaterials = ScreenMeshRenderer.materials; tempMaterials[1] = ScreenCompletionMaterial; ScreenMeshRenderer.materials = tempMaterials;

This should work :slight_smile:

2 Likes