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
0
Question by shockreel · Jan 03, 2014 at 12:06 AM · textureingmaterial.

Changing textures on multiple materials shared by a mesh renderer

Hello. I am working on a project and using JavaScript to change the main textures on materials at the push of a GUI button (renderer.material.mainTexture= whatever). I've been keeping it really simple and so far, things have been moving along perfectly and without any trouble.

However, I just encountered a mesh renderer on a single mesh that is using two separate materials. I apply my texture switcher script and just as I expected, it is only switching the texture on one of the two materials.

I'm attaching the script I am using as well as a screen grab of my inspector. Again, this is working perfectly on everything else except the mesh with 2 materials. Is there a way to change the texture on that second material?

var RaceRedTexture:Texture; var SilverMetallicTexture:Texture; var DarkBlueTexture:Texture; var FrozenWhiteTexture:Texture; var PantherBlackTexture:Texture; var TectonicSilverTexture:Texture; var SchoolbusYellowTexture:Texture; var MidnightSkyTexture:Texture; var BurnishedGlowTexture:Texture; var SolarYellowTexture:Texture; var DeepImpactBlueTexture:Texture;

function AssignColor (chg:int)

{ switch(chg) { case 1:// Race Red gameObject.renderer.material.mainTexture=RaceRedTexture; break;

     case 2:// Silver Metallic
     gameObject.renderer.material.mainTexture=SilverMetallicTexture;
     break;
     
     case 3:// Dark Blue
     gameObject.renderer.material.mainTexture=DarkBlueTexture;
     break;
     
     case 4:// Frozen White
     gameObject.renderer.material.mainTexture=FrozenWhiteTexture;
     break;
     
     case 5:// Panther Black
     gameObject.renderer.material.mainTexture=PantherBlackTexture;
     break;
     
     case 6:// Tectonic Silver
     gameObject.renderer.material.mainTexture=TectonicSilverTexture;
     break;
     
     case 7:// Schoolbus Yella
     gameObject.renderer.material.mainTexture=SchoolbusYellowTexture;
     break;
     
     case 8:// Midnight Sky
     gameObject.renderer.material.mainTexture=MidnightSkyTexture;
     break;
     
     case 9:// Burnished Glow
     gameObject.renderer.material.mainTexture=BurnishedGlowTexture;
     break;
     
     case 10:// Solar Yellow
     gameObject.renderer.material.mainTexture=SolarYellowTexture;
     break;
     
     case 11:// Deep Impact Blue
     gameObject.renderer.material.mainTexture=DeepImpactBlueTexture;
     break;
 }

}

alt text

screen shot 2014-01-02 at 2.52.20 pm.png (135.0 kB)
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

1 Reply

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

Answer by shockreel · Jan 03, 2014 at 04:25 PM

I answered my own question. It took some digging to find but it was super simple. Multiple materials on a mesh are handled in Unity as an array. Here's the solution for anyone who needs it: (I'm just posting a small chunk of my code.)

function AssignColor (chg:int)

{

 switch(chg)
 {
     case 1:// Race Red
     gameObject.renderer.materials[0].mainTexture=RaceRedTexture1;
     gameObject.renderer.materials[1].mainTexture=RaceRedTexture2;
             break;
     }

}

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 Xenocide · Oct 25, 2016 at 03:37 PM 0
Share

Anyone know how to do this for GetComponent().material? I have two materials in my renderer. How do I specify the other one when all it allows me to do is choose the first instantiated??

I can't find an answer. I usually figure it out while I post a problem so I'll update here if I figure it out.

EDIT: Okay it was GetComponent().materials to be able to choose which material. So GetComponent().materials[1] will choose my second element (as in second material), [0] will choose the first element (first material).

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

19 People are following this question.

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

Related Questions

Texture doesn't get applied on .FBX file 3 Answers

Why does applying a material to a object break batching? 2 Answers

Unity Models Question 0 Answers

Gray line at top of the texture 0 Answers

Blender sphere object not rendering materials correctly in unity 1 Answer


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