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 D_e_l_t_a · Nov 23, 2011 at 05:10 PM · materialstextures

Accessing multiple texture materials on objects

How to get to and modify the separate parts of a multi textured object ? For example, I have a tapered cube created in my 3D modeling app. The 6 sides have all been separately assigned textures, named Topside, Bottomside, Left, Right, Front and Back. All of these are re-created as Materials when exported to Unity.

In Unity, how can I access e.g the Front Material and change it's Color property from within a script ?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by hijinxbassist · Mar 08, 2012 at 11:29 PM

If you want to access each material on your object, you can use something like this..

 function Awake()
 {
     var materials:Array=renderer.materials;
 
     if(tag=="Enemy")
     {
         materials[0].color=Color.black;
         materials[1].color=Color.red;
     }
 }

I stumbled on this post and figured i add my 2 cents(or materials in this case)

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 hijinxbassist · Mar 10, 2012 at 10:55 PM 0
Share

I did a test on this(only thing in the script)using #pragma strict. Seems it wont work the way i laid it out. By removing the var materials and replacing the 'materials' vars with the actual line renderer.materials fixes it.

#pragma strict
function Awake()
{
    if(tag=="Enemy")
    {
       renderer.materials[0].color=Color.black;
       renderer.materials[1].color=Color.red;
    }
}
avatar image hijinxbassist · Mar 24, 2012 at 06:35 AM 0
Share

Futher update to my answer since Unity has added #pragma strict in the beggining of ALL new scripts(not a bad idea really!). Here is how my first example would be written in strict typing for all you out there in cyberland!

 #pragma strict

 var mat:$$anonymous$$aterial[];

 function Awake()
 {
     mat=renderer.materials;
 }

Then you can just refer to them as

 mat[0].mainTexture=myTexture;
 mat[1].color=myColor;

Hope this helps some of you out. Feel free to +1 if i saved your day and prevented further hair-pulling! LOL

avatar image
0

Answer by DaveA · Nov 23, 2011 at 05:14 PM

The import process should have created 6 different Materials right? So you just change them like this http://unity3d.com/support/documentation/ScriptReference/Material-color.html

 Front.color = new Color(.2,.5,.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 D_e_l_t_a · Nov 23, 2011 at 07:11 PM 0
Share

How do I access the material labelled as 'Front' ? Is it assigned as a variable first ? The imported object has assigned all the 6 materials, but I can't get to their names without an error being thrown.

avatar image
0

Answer by Vienna · Nov 24, 2011 at 09:38 PM

You can access the materials like this http://unity3d.com/support/documentation/ScriptReference/Renderer-materials.html

So for example:

 var mats = go.renderer.materials;
 for (mat in mats) {
     mat.color = Color.red; 
 }
 
Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Material with two textures 0 Answers

Set Material Texture Border 1 Answer

Cannot implicitly convert type `UnityEngine.Texture2D' to `UnityEngine.Material' 2 Answers

Random Tiling Textures 0 Answers

Unity Materials and UV coordinate help 3 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