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
0
Question by Guardian2300 · May 20, 2014 at 10:14 PM · materialcolormemorymemory-leak

Materials Count creates Memory Leak

Hello, I've noticed this issue and can't figure out if this is due to my coding or unity bug. I change the material on the object a lot and my materials count increases and increases.

Here's my sample code

 if(renderer.material.name != Green.name + " (Instance)")
 {
     renderer.material = Green;
 }

this is in my update function. This would happen every time I highlight it but it this seems to increase the count. Does anyone else had this problem, if so how did you solve it. Thank you

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

2 Replies

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

Answer by DMGregory · May 22, 2014 at 06:11 PM

Note that unlike most other objects, Material instances are not automatically disposed when no gameObject/component is using them.

Anytime you create a material instance through script (which happens automatically whenever you use renderer.material instead of renderer.sharedMaterial), cache a reference to it, something like this:

 _cachedMaterial = Instantiate(sourceMaterial) as Material;
 renderer.sharedMaterial = _cachedMaterial;

If you later change to a different material or destroy the renderer, and won't need _cachedMaterial again, manually dispose of the instance (since this instance was created through script, you won't get a "Destroying assets is not permitted" error):

 Destroy(_cachedMaterial);

(Use DestroyImmediate if this is an Editor script)

This will prevent material instances from leaking.

If you don't need to change parameters on the material per-instance, use renderer.sharedMaterial instead - this will allow multiple objects to share one material reference, rather than instantiating a unique copy for each one.

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
avatar image
0

Answer by Xumbu · May 20, 2014 at 10:16 PM

  1. use Equals() to compare strings (sry java background)

  2. try

    renderer.materials = new Material[1]; renderer.material[0] = Green;

Comment
Add comment · Show 7 · 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 Guardian2300 · May 20, 2014 at 10:21 PM 0
Share

tried renderer.material = new $$anonymous$$aterial(Green); but it seems to double the material count

avatar image Xumbu · May 20, 2014 at 10:22 PM 0
Share

yes. because of that create a new array

avatar image Guardian2300 · May 20, 2014 at 10:24 PM 0
Share

does renderer.materials store the extra materials to change between them

avatar image Xumbu · May 20, 2014 at 10:25 PM 0
Share

I'm not sure but it seems like the material 'setter' just adds the material to the array.

and probably material 'getter' returns the first element of this array. otherwise i can't explain that problem.

try it out

avatar image Guardian2300 · May 20, 2014 at 10:27 PM 0
Share

Thanks, I don't know why but this seems to be happening to me with the materials all the time now

Show more comments

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

22 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

Related Questions

Material doesn't have a color property '_Color' 4 Answers

Changing two different objects renderer colour 1 Answer

EXC_BAD_ACCESS (SIGABRT) from Unity::Material::CreateMaterial 0 Answers

Changing material color creates a memory leak 1 Answer

m_Memory.renderTextureBytes<0 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