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 dissidently · Dec 18, 2010 at 01:42 PM · prefabprefabsmaterialsassign

Assign different materials to a range of same prefab?

this is surely basic, but the help files don't make sense to me, and can't find a matching answer in here.

I have a cube prefab. I create 5 of them at launch, each named uniquely, using an array that iterates through a for loop up to the 5. and, while placing them, I want to assign a different material to each one. The materials are named "instrument1" thru "instrument5" and currently in the Project.

Here's what I thought would be the manual/reference page to show how to do this, but it makes no sense to me.

http://unity3d.com/support/documentation/ScriptReference/Renderer-material.html

var materials is an array of Material[]???? which materials? What are their names? How are they being referenced/arrayed?

and why is their a "sharedMaterial", surely this is not being shared.

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
1
Best Answer

Answer by Eric5h5 · Dec 18, 2010 at 04:13 PM

Just assign a material to each prefab. It would be easier if you used an array of materials instead of separate variables. As for "which materials", they are whatever materials you assign in the inspector. They're referenced like any other array (by number).

var prefab : GameObject; var materials : Material[];

function Start () { for (i = 0; i < materials.Length; i++) { var clone = Instantiate(prefab); clone.renderer.material = materials[i]; } }

sharedMaterial is for assigning a single material to multiple objects, so it's shared. Changing the sharedMaterial of one object would cause the material on all of them to be changed.

Comment
Add comment · Show 14 · 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 dissidently · Dec 19, 2010 at 08:56 AM 0
Share

Ok, I'm still stuck with the same problem. I want to assign a different material to each of the Instantiations of the one prefab. Otherwise, yes, I would apply the materials in the editor/inspector. So my question is still sort of the same. WHERE and WHAT are the materials that your array materials[] points to in the editor? Are they called material1, material2, material3? And where are they that the script can find them?

avatar image dissidently · Dec 19, 2010 at 09:07 AM 0
Share

actually, one more thing/question. It appears "$$anonymous$$aterial" with a capital letter is a reserved word, from the color in your code. A class. I'm sorry, but the scripting components of materials and Unity have me completely baffled. Is beyond simplistic.

Please try to explain in non shorthand, where I'm wrong. You've created a variable called materials, and it contains all the information of all materials in the editor in an array the length of all materials in the editor??? Then you create enough clones of the object to useup all the materials in the editor?
Is this right?

avatar image dissidently · Dec 19, 2010 at 09:11 AM 0
Share

If I make a range of materials so they're in a folder called "clone_materials" which sits nested inside a parent folder called "materials" in my project, and the materials are named clone_material_1, clone_material_2 ... thru 8. How do I assign each one of these materials each one of 8 clones of the same prefab?

avatar image Eric5h5 · Dec 19, 2010 at 09:13 AM 0
Share

The code I wrote already assigns a different material to each instantiation of the prefab. The materials are in the array, when you add them. If you don't add anything, the array is empty. Look at the script in the inspector, open the materials variable if necessary, and add items to it.

avatar image dissidently · Dec 19, 2010 at 09:15 AM 0
Share

One more thing, Eric, THAN$$anonymous$$YOU for explaining shared$$anonymous$$aterial. That ter$$anonymous$$ology was doing my head in. Is there somewhere online the developers of Unity set out their methodology, rationality and reasoning behind their decisions about design of Unity?

Show more comments

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

No one has followed this question yet.

Related Questions

How can I make that everyTime I change the texture of material, it won't change texture of every Instantiated Object that has that material 2 Answers

how do I assign a prefab's child to another prefab's script's public variable? 1 Answer

How to assign prefab at runtime? 1 Answer

New prefab system problem 1 Answer

Prefabs losing Material connections after re-opening scenes. Bug? 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