Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by Deoxyz · Dec 13, 2009 at 09:05 AM · texturematerialchange

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
9
Best Answer

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Deoxyz · Dec 13, 2009 at 02:31 PM 0
Share

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

avatar image Ashkan_gc · Mar 11, 2010 at 06:06 PM 0
Share

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.

avatar image
6

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image wkmccoy · Sep 08, 2010 at 04:32 PM 0
Share

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

avatar image cregox · Jan 27, 2012 at 05:18 PM 0
Share

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!

avatar image
1

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;

}

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Aurorem · Oct 17, 2012 at 10:31 AM 0
Share

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

avatar image cregox · Oct 17, 2012 at 02:09 PM 0
Share

@Aurorem in the editor.

avatar image
1

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]

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cregox · Jul 20, 2013 at 04:15 AM 0
Share

Yes, this have already been commented...

avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tasbar · Mar 12, 2010 at 02:56 PM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges