- Home /
Change Material of an Object.
Hi,
How can I change a material of an object in Unity when I press a key? I know how I do something while pressing a key, but can't find how to change materials.
For example: I have a weapon and it has different abilities. Each abilitie has a color and the gun has to change textures. I want to change material(not only textures) because material sets textures and opacity of an object.
I'm using javascript to do this.
Thanks in advance.
Answer by pkamat · Dec 13, 2009 at 09:44 AM
What i do i maintain a list of materials in the object and change the material by simply settings renderer.material to the one is the list.
for eg: this is my set material function
void SetMat(int nIdx)
{
renderer.material = m_aMaterials[nIdx];
}
Not sure if this approach is the best though
Thanks for the help. With render.material I could switch between materials. I made som vars of type material and then with a counter add 1 eachtime you press 'e'. When counter is number 1 = $$anonymous$$aterial1, when 2 = $$anonymous$$aterial2. Didn't got a list of materials, but I can choose my materials when I select my object in Hierarchy, becouse they are not private vars.
Thanks!:D
please put the codes in a code block. it's simple just press the code button and type your code and press shift+enter for going to the next line and at end press enter or just after typing your code select all of it and press the code block button. thank you so much.
Answer by tony oakden · Jun 05, 2010 at 12:04 AM
Hi,
Yes for some reason changing the individual materials in the array doesn't work. not sure why that is...
However changing the complete arrary does work.
So if you have an array of materials called: alternateMaterials; The following will not work:
body.renderer.material[0] = alternateMaterials[0];
However this will work:
body.renderer.materials = alternateMaterials;
At least for me!
If you want to change just one of hte materials at run time then you'll need to copy the list of materials out of the render object into a new array, change the material you want to change then reassign the renderer material array to the new one as above. Make sense?
regards,
Tony Oakden
render.materials[x].shaders = strored_shader
render.material.shaders does the first material only render.materials is an array of all materials on the object renter.materials[x].YYYY where YYYY = material variables such as color
This is what makes sense from the Renderer.materials doc, where it 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.". Thanks!
Answer by Mickman · Aug 23, 2012 at 08:18 PM
var material1 : Material; var material2 : Material;
function OnChangeMaterial() { // toggle between the two materials if( renderer.material == material1 ) renderer.material = material2; else renderer.material = material1;
}
nice script but how do you indicate wich texture is $$anonymous$$aterial for the variable material1 and wich is material2
Answer by adiblev · Jul 19, 2013 at 11:55 PM
Hey guys - the reason why Unity will NOT allow you to switch the materials is because Unity is passing you a copy of the array and so you need to set the array in order to apply the changes.
The following sentence is taken from the Unity docs:
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.
Here is the link:
[http://docs.unity3d.com/Documentation/ScriptReference/Renderer-materials.html][1]
Answer by Scott 4 · Mar 11, 2010 at 05:37 PM
Were you able to do anything with renderer.materials[i]
I'm having the same issue (I can do it one at a time) but I can't seem to loop over the Materials Array and make any changes.
example: renderer.material=shieldMaterial; //Works every time
renderer.materials[i] = shieldMaterial; //Doesn't work
When you don't answer the question, please use 'add comment'
Your answer
Follow this Question
Related Questions
How to create a GUI button to change a character's texture in real-time? 2 Answers
Cost of changing texture 0 Answers
Changing GameObject texture? 4 Answers
How can i change detail texture? 1 Answer
referring to a particular material 0 Answers