Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by toby-loveridge · Nov 07, 2015 at 11:37 PM · scripting problemmaterialsrenderer

how can I get my prefab to have a different material on instantiation

Im stuck on how i can get my game object prefab to have a randomly selected material out of three different ones on each instantiation. This is the code i have:

 void Start ()
     {
         rend = GetComponent<Renderer>();
         mInital = rend.material;
         myTransform = transform;
 
         colourSelect = Random.Range(0, 3);
         switch (colourSelect)
         {
             case 1:
                 mInital = mRed;
                 break;
             case 2:
                 mInital = mBlue;
                 break;
             case 3:
                 mInital = mYellow;
                 break;
         }

I would appreciate it if anyone could help. Im just trying to teach myself everything about Unity :)

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 Statement · Nov 07, 2015 at 11:49 PM

I guess it should end with rend.material.color = mInitial; But also, keep in mind that Random.Range(0, 3) can return values 0, 1 or 2. Not 1, 2 or 3. Your code will wither keep the original color or use red/blue but never yellow.

You could also make it a little easier to change:

 public Color[] colors = { Color.red, Color.blue, Color.yellow };
 
 void Start()
 {
     var renderer = GetComponent<Renderer>();
     if (colors.Length > 0)
         renderer.material.color = colors[Random.Range(0, colors.Length)];
 }

Now you don't have to limit yourself to 3 colors.

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 toby-loveridge · Nov 08, 2015 at 03:40 PM 0
Share

Thank you, that code was really helpful. I am just wondering that doesn't take the materials I made, right? If I had a different Texture or material I would have to do this another way im assu$$anonymous$$g.

avatar image Statement toby-loveridge · Nov 10, 2015 at 03:17 PM 0
Share

I don't understand what you mean. It'll change the color of the material assigned to the renderer of the game object. It doesn't "take" anything. It modifies whatever is on there.

Actually, it doesn't modify the source material, it creates a copy of the material and modifies it (otherwise it would be modifying the original asset, which would likely be annoying as it would modify every single object using that material).

But it doesn't matter which material the renderer happens to have at Start. You can change material or texture. The only thing that matters in this case is that the shader the material is using actually make use of the main color property.

If you want to replace the material of the renderer ins$$anonymous$$d of modifying the material, you can just do renderer.shared$$anonymous$$aterial = materials[Random.Range(0, materials.Length];

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

33 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 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

Using multiple materials on a 2d mesh 0 Answers

Gizmos not working in Unity 5.5.1f1 0 Answers

Model not rendering//no textures. 0 Answers

What right way to SetTextureOffset of material which has dynamic count owners? 0 Answers

I create material with script but it does not render right 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