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
1
Question by Hoeloe · Jul 15, 2013 at 05:01 PM · errormaterialsrenderer

Setting materials at runtime

Hey, I'm trying to change the material of a group of objects at runtime, and I'm coming across a rather strange issue. I have a method which gets me a list of the Transforms of all the objects I need to change (by recursively calling itself over the Transform iterator), and then I'm looping through that and setting the materials array (the entire array). I'm seeing literally no change to the game, but when I ran some diagnostic prints, I found that the renderer seems to have set the materials, but it looks like it's immediately setting them back again. Can anyone tell me what I'm doing wrong here? This is the code I'm using:

  Transform[] ts = parent.GetRenderTransforms().ToArray();
                 for (int i = 0; i < ts.Length; i++ )
                 {
                     materials.Add(ts[i], ts[i].renderer.sharedMaterials)
                     Material[] ms = new Material[ts[i].renderer.materials.Length];
                     for (int j = 0; j < ms.Length; j++)
                     {
                         ms[j] = hourai;
                     }
                     ts[i].renderer.materials = ms;
                 }

Where "hourai" is a variable holding the material I'm trying to set, and "materials" is a Dictionary allowing me to map the original materials back. I'm getting no errors whatsoever, and the debugging I've done all seems to point to it working correctly. Can anyone help here?

EDIT:

I've found that if I don't add the values to the dictionary, then it all works. Can anyone think of a reason that this might cause a problem?

Comment
Add comment · Show 3
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 perchik · Jul 15, 2013 at 05:07 PM 0
Share

Can you show us the code where the materials dictionary is created? $$anonymous$$y guess is that you're setting and using values from the dictionary but not actually changing them in the dictionary.

avatar image Bunny83 · Jul 15, 2013 at 05:37 PM 0
Share

Are you sure you want this line:

 ts[i].renderer.materials = ms;

I guess this would make more sense:

 ts[i].renderer.shared$$anonymous$$aterials = ms;

Assigning to the materials array will create duplicates of your materials for each renderer.

avatar image Hoeloe · Jul 16, 2013 at 04:00 PM 0
Share

@Bunny83 - For the record, I was using materials and not shared$$anonymous$$aterials on purpose. The material I was assigning was set to dynamically change colour, and I have other instances of the object in the scene, and I wanted each to act independently, so materials was more appropriate.

1 Reply

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

Answer by Hoeloe · Jul 15, 2013 at 05:10 PM

So it turns out the condition for resetting the materials wasn't set correctly, so it was immediately running through the Dictionary and resetting all the textures. It's fixed now.

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

17 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

Related Questions

Index array is out of range (following H&S tut) 2 Answers

Changing the materials in Editor Mode Without Getting the leak Error 3 Answers

Changing two different objects renderer colour 1 Answer

Help with changing spririterenderer's sprite 2 Answers

Emissive Material control via script (code) 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