|
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.
(comments are locked)
|
|
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
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 = 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)
|
|
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
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)
|
|
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'
Mar 12 '10 at 02:56 PM
tasbar
(comments are locked)
|
|
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 Material for the variable material1 and wich is material2
Oct 17 '12 at 10:31 AM
Aurorem
(comments are locked)
|
