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 /
avatar image
0
Question by elijahFTL · Feb 14, 2017 at 05:54 AM · c#materialcolor changedictionary

Changing material color from dictionary (C#)

I've been working on an application in which the user loads a 3D model representing a part (important for context as I delve into the situation) into the environment and has the ability to adjust the transparency of that model. I have a working solution for this currently:

 Material partMat =
     app.currentPart.GetComponent<MeshRenderer>().material;
 Color partColor = partMat.color;
 partColor.a = val;
 partMat.color = partColor;

This solution loads the GameObject containing the model ( app.currentPart) and successfully changes its alpha channel to val, the parameter of the slider callback method from which this code was taken.

However, I have also recently implemented a change to my application such that the part can be broken up into its features, each represented by a GameObject containing a 3D model of its own (each such object is a sibling of one another under an empty GameObject). The only real difference in structure is that rather than referring to a sole GameObject app.currentPart, I refer to app.currentFeatures, which is a Dictionary<string, GameObject>. I want to adjust the transparency the same way. Some Googling led me to approach it this way:

 List<string> keys = new List<string>(app.currentFeatures.Keys);
 foreach (string featName in keys)
 {
     Material featMat =
         app.currentFeatures[featName].GetComponent<MeshRenderer>().material;
     Color featColor = featMat.color;
     featColor.a = val;
     featMat.color = featColor;
     app.currentFeatures[featName].GetComponent<MeshRenderer>().material = featMat;
  }

However, this yields no changes to the transparency of these models at runtime. I know that app.currentFeatures is correctly populated because if I print featName each iteration, it is correct. My suspicion is that this code is editing a copy of the GameObject rather than the one loaded into the environment, but I feel like I've tried pretty much all the different angles for getting the object itself.

Since submitting this question for moderation, I tried another approach based on the object hierarchy rather than my script variables/containers:

 foreach (MeshRenderer featMesh in app.partContainer.GetComponentsInChildren<MeshRenderer>())
 {
    Material featMat = featMesh.material;
    Color featColor = featMat.color;
    featColor.a = val;
    featMat.color = featColor;
 }

My thought was that referring directly to the children of app.partContainer (the empty GameObject holding the part/features as children) would solve this problem, but unfortunately, it did not.

UPDATE: I have since changed the specifications of my application significantly such that each feature has a type and each type corresponds to a uniquely colored material. When I implemented this, suddenly the problem went away entirely. I still would like to see this original question answered just in case others run into the same issue, though.

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 hexagonius · Feb 15, 2017 at 04:15 PM 0
Share

ok, so at first it was app.currentPart with a $$anonymous$$eshRenderer and just one $$anonymous$$esh, right? Then you split that up and placed each individual GameObject with its $$anonymous$$eshRenderer part into your app.currentFeatures Dictionary along with a name, ok? Then you ran a foreach loop over app.partContainer, asking for its child $$anonymous$$eshRenderers.
So my question is, did either iterations (dictionary or children $$anonymous$$eshRenderer) iterate the $$anonymous$$eshRenderers? Did the code that should reassign the materials execute?

avatar image elijahFTL hexagonius · Feb 15, 2017 at 04:30 PM 0
Share

Yes, the code definitely executed during each iteration, and it iterated correctly as well (I had it print out the name of the object containing the $$anonymous$$eshRenderer).

avatar image hexagonius elijahFTL · Feb 15, 2017 at 05:04 PM 0
Share

then either all materials changed to the same alpha, the material read did not make use of an alpha, why it stayed opaque, or I don't know.
you said you provided your own set of materials. how do those compare to the ones already applied?

0 Replies

· Add your reply
  • Sort: 

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

8 People are following this question.

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

Related Questions

Create Staining effect 0 Answers

Multiple Cars not working 1 Answer

How to make enemies flash on hit 2 Answers

Distribute terrain in zones 3 Answers

Unity3D -Trying to decrease color values when it equals to zero and curious result 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