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 Bruno2907 · Dec 29, 2016 at 06:28 AM · meshrenderermaterial renderersharedmaterial

What happens to the instantiated material after i change the .sharedMaterial?

I have an object (wall) that switch between 4 diferent materials (using .sharedMaterial) during the gameplay. But when one of them is active the player can interact with it, when this happen I use lerp to change the color over time. I do this using .material so Unity create a new instance of this material.

What will happen later when I switch again the .sharedMaterial? Will the instance auto destroy or I have to do it? And when the cycle ends and I come back to this specific material, will Unity create another instance?

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
1
Best Answer

Answer by AlwaysSunny · Dec 29, 2016 at 06:34 AM

It's not just creating one new instance of the material - it's creating a new instance of the material every frame the Lerp is occurring. When changing material properties over time, you should use the Get- and SetPropertyBlock API calls:

https://docs.unity3d.com/ScriptReference/Renderer.SetPropertyBlock.html

The instances you create by directly changing properties on a material (without using these methods) are not destroyed automatically, they are leaked. You can clean up all of them at once with Resources.UnloadUnusedAssets();

https://docs.unity3d.com/ScriptReference/Resources.UnloadUnusedAssets.html

Because this is a very expensive method to call, you should use SetPropertyBlock whenever possible, which avoids leaking orphan materials in the first place.

The sharedMaterial is actually the same material in your project's assets. You'll see that changes to the sharedMaterial persist even after exiting playmode. It's safe to directly change the properties of the sharedMaterial as often as you want without using the SetPropertyBlock approach.

I'm not sure how to change an instantiated material back to the sharedMaterial. You could try simply assigning the sharedMaterial to the material property?

 // I don't know if this'll work, it's never actually come up for me
 renderer.material = renderer.sharedMaterial;

If someone wants to chime in, that'd be great!

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 Bruno2907 · Dec 30, 2016 at 12:54 AM 0
Share

I didn’t know about this SetPropertyBlock, thanks for showing me. I fixed the code to use this method. It worked flawless, I used this in the Lerp and after to ‘reset’ I just had to do .SetPropertyBlock(null);

Actually, just doing .shared$$anonymous$$aterial = new$$anonymous$$aterial would work even after I created an Instance, and then the instance would be leaked like you said.

avatar image Bunny83 · Dec 30, 2016 at 01:32 AM 0
Share

You are basically right, but you won't create a new material every frame. That would happen when you reassign the shared material every frame as well. If a renderer uses a shared material and you access the material property, Unity will automatically instantiate the shared material and use that instance from now on. So the second access of the material property won't instantiate a new material as long as the shared material is still the same.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Material vs. SharedMaterial - difference in SETTING? 1 Answer

Why does reading MeshRenderer.material change MeshRenderer.sharedMaterial? 1 Answer

How to check if two objects share the same material within an if statement? 0 Answers

How to make a gameobject share the material of the collider it collides with? 2 Answers

Enabling Multiple MeshRenderer at one time 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