x


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.

more ▼

asked Dec 13 '09 at 09:05 AM

Deoxyz gravatar image

Deoxyz
94 2 3 7

(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

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

more ▼

answered Dec 13 '09 at 09:44 AM

pkamat gravatar image

pkamat
438 11 12 20

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 = Material1, when 2 = Material2. 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

Dec 13 '09 at 02:31 PM Deoxyz

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.

Mar 11 '10 at 06:06 PM Ashkan_gc

Please don't post an Answer unless you are offering a solution to the original question. I will convert it to a comment.

Apr 17 at 10:51 AM whydoidoit
(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Jun 05 '10 at 12:04 AM

tony oakden gravatar image

tony oakden
53 3 3 4

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

Sep 08 '10 at 04:32 PM wkmccoy

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!

Jan 27 '12 at 05:18 PM Cawas
(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Mar 11 '10 at 05:37 PM

Scott 4 gravatar image

Scott 4
16

When you don't answer the question, please use 'add comment'

Mar 12 '10 at 02:56 PM tasbar
(comments are locked)
10|3000 characters needed characters left

var material1 : Material; var material2 : Material;

function OnChangeMaterial() {    // toggle between the two materials    if( renderer.material == material1 )      renderer.material = material2;   else      renderer.material = material1;

}

more ▼

answered Aug 23 '12 at 08:18 PM

Mickman gravatar image

Mickman
16 4 8 10

nice script but how do you indicate wich texture is Material for the variable material1 and wich is material2

Oct 17 '12 at 10:31 AM Aurorem

@Aurorem in the editor.

Oct 17 '12 at 02:09 PM Cawas
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2279
x842
x389

asked: Dec 13 '09 at 09:05 AM

Seen: 30984 times

Last Updated: Apr 17 at 10:51 AM