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 Stormizin · Apr 17, 2012 at 03:04 PM · instantiatematerialassign

How to clone a material in each instantiated prefab as Unique temporary material.

Hello guys, i tried everything to instantiate a material into a clone of a prefab. I tried to attach a script into a Car Prefab who can randomize colors, in each cloned prefab i need to put a new material or a temporary material with one color each.

I tried to change the material who references the prefab but all the cars change to the same color in each start of my coroutine.

 private void RandomCarColor(){
 rlColor = carColor[randomColorNumber];
      string shaderText =
     "Shader \"Alpha Diffuse\" {" +
     "Properties { _Color (\"Main Color\", Color) = (1,1,1,0) }" +
     "SubShader {" +
     "    Tags { \"Queue\" = \"Transparent\" }" +
     "    Pass {" +
     "        Blend One One ZWrite Off ColorMask RGB" +
     "        Material { Diffuse [_Color] Ambient [_Color] }" +
     "        Lighting On" +
     "        SetTexture [_Dummy] { combine primary double, primary }" +
     "    }" +
     "}" +
     "}"; 
      renderer.material = new Material( shaderText );
              renderer.material.color = rlColor;
      clonedMaterial.color = rlColor;
 

What help i need: How to, assign a temporary material for each instance i spawn from this original car object.

I can saw the new material(Instantiated) in the inspector but it not assigned the color cars dont change for a unique color each one. Someone can help? Thanks.

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

4 Replies

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

Answer by Stormizin · Apr 17, 2012 at 09:04 PM

I did, something more easily, i opened the maya modeling, and change my car model, i combine all the polygons into one, imported to the unity again and created a new car prefab puting the Unique Mesh into the prefab, then i created a script who randomize the color at the Start() function. Thanks a lot bro for your time.

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
avatar image
4

Answer by Piflik · Apr 17, 2012 at 05:06 PM

When you alter a property of a material during runtime, Unity automatically creates a clone of the material and uses it for the current prefab/clone only. The other objects in the scene using the same material won't be affected, unless you use:

 renderer.sharedMaterial
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
1

Answer by Lttldude · Apr 17, 2012 at 03:57 PM

I think you may be over complicating this or I don't fully understand.

If you are trying to just create a new color for each car, then do something like this:

 var prefab : GameObject;
 var colors : Color[];
 
 function Start()
 {
     for(var i=0; i<5; i++)
     {
         var instPrefab : GameObject = Instantiate(prefab, Vector3(5*i,5*i,0), Quaternion.identity);
         instPrefab.renderer.material.color = colors[Random.Range(0, colors.Length)];
     }
 }
 

Tell me if this doesn't help.

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
0

Answer by bombsquare · Feb 06, 2016 at 02:30 PM

You maybe looking for procedural materials that can be created at runtime?

http://docs.unity3d.com/Manual/class-ProceduralMaterial.html

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

7 People are following this question.

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

Related Questions

Material Switching Upon Instantiation. over my head 0 Answers

Checking if object intersects? 1 Answer

Material doesn't have a color property '_Color' 4 Answers

Missing Material in AssetBundle Prefab 3 Answers

Cannot Assign Material To Object 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