• 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
Question by Jasper_Moore · Jul 09, 2015 at 09:17 AM · shaderrenderingmaterialstransparent shader

Altering Material through Script Causes Transparent Rendering Error - C#

I simply need to swap a material on an object with multiple materials. If I run a for loop like so:

public Material newMat;

myMats = render.sharedMaterials

for(int i = 0; i < myMats.length; i++) {

  if(myMats[i].someProperty == someCheck)
  {
     myMats[i] = new Material(newMat);
  }

}

... or any variation of this, the Material simply doesn't change.

Alternatively, I thought I could use something like this, changing the material's properties, rather than the material altogether:

  if(myMats[i].someProperty == someCheck)

  {
     myMats[i].shader = newMat.shader;
     myMats[i].CopyPropertiesFromMaterial(newMat);
  }

... This almost does exactly what I need, but if the material changes from an opaque rendering mode to a fade, for example, all of the properties change, but the material doesn't render properly until I manually change something in the inspector (for example, nudging the color bar). After I touch the material manually, it corrects itself and renders as it should. This isn't helpful though, as it should be automatically updated in game.

Is this a known Unity bug, or is there a way to update/refresh the shader after applying changes? Any feedback is appreciated. Here are some screenshots to better explain my problem. The first is the material before the change, the second is how it SHOULD look after the change, but the third is my actual result.

alt text

example.jpg (431.3 kB)
Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image

Answer by Bunny83 · Jul 09, 2015 at 09:33 AM

Uhm, where do you actually assign the material(s) to your renderer(s)? "myMats" seems to be an array of Material references. When you exchange a material in that array, the material that your renderer uses isn't changed.

If you use the ".materials" or ".sharedMaterials" array property of your renderer, you get a new array containing the used material. If you want to change them you have to assign the array back:

 Material[] myMats = renderer.materials;
 
 // do your changes
 
 renderer.materials = myMats;

Keep in mind that when using ".materials" or ".material" Unity will create a copy of each material just for this instance. This might be relevant for drawcalls. The "sharedMaterial(s)" contain direct references to the actual material(s). However exchanging a material in the sharedMaterials array has to be done the same way:

 Material[] myMats = renderer.sharedMaterials;
 
 // do your changes
 
 renderer.sharedMaterials = myMats;
Comment

People who like this

0 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 Bunny83 · Jul 09, 2015 at 09:36 AM 0
Share

ps: about "CopyPropertiesFromMaterial", this might be some kind of bug in that method. Maybe they haven't considered the new default shader and some settings are lost.

avatar image Jasper_Moore · Jul 09, 2015 at 03:44 PM 0
Share

Thanks for the reply! Unfortunately, it didn't solve my problem.

myMats = renderer.sharedMaterials. Therefore, the changes appear to be automatically applied to the renderer. I did implement your suggestion though, hoping for different results, but I get the same result no matter which scripting path I take. I've attempted a material swap in the simplest form possible, with one object and two materials, as you see here, but it happens either way.

I'm pretty confident this is a bug in Unity 5, because I've been doing material swaps since, literally, my first Unity game three years ago, and I've never come across this issue. Still hoping I'm wrong and simply overlooking something, though.

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Transparency cutting opaque mesh 0 Answers

Distortion Shader does not render transparent objects (Shader Graph) 2 Answers

Sequential shaders. Pass the output color of a shader to the next one. 0 Answers

set gameobject in front of all others and don't have own faces appear in front of each other 1 Answer

Unity 3D Vs 3DS Max Default Models Colors Variance 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