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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by mokoton · Sep 27, 2014 at 02:24 PM · garbage collection

garbage collection: material

below is my code. i want to know if the material i created is collected at the beginning of the next update cycle.if not how could i collect it?

     public Color objectColor;
     private Material material;
     void Update()
     {   
         material = new Material(renderer.sharedMaterial);
         material.color = objectColor;
         renderer.sharedMaterial = material;
     }
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
0
Best Answer

Answer by dsada · Sep 27, 2014 at 05:17 PM

They are not destroyed but you can do it manually like this:

 public Color objectColor;
 private Material material;
 
     void Update()
     {   
         Material tmpMat = renderer.sharedMaterial;
         material = new Material(renderer.sharedMaterial);
         material.color = objectColor;
         renderer.sharedMaterial = material;
         Destroy(tmpMat);
     }

However let me ask you why do you create a new material? Is it really necessary? Instead of this code that we can see you can calmly wrtite something like this:

     public Color objectColor;
     void Update()
     {   
         renderer.sharedMaterial.color = objectColor;
     }


And another question: why do you need to assign the color every frame? Does it change? because if not, you can assign once and you've got even less problem:

     public Color objectColor;
     void Start()
     {   
         renderer.sharedMaterial.color = objectColor;
     }
Comment
Add comment · Show 1 · 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 mokoton · Sep 27, 2014 at 05:35 PM 0
Share

yea thats what i was looking for as to your two questions when i use sharedmaterial it changes color for everything and i dont want that. also i wanted to see the color change(in editor mode)thats why its in update but later on it will be moved to start.

avatar image
0

Answer by Eric5h5 · Sep 27, 2014 at 02:32 PM

It will never be garbage collected because it's in use. The point of garbage collection is that it removes objects that aren't being referenced by anything. But even if it wasn't being used, no Unity objects are garbage collected, only Mono objects are.

Comment
Add comment · Show 1 · 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 mokoton · Sep 27, 2014 at 04:24 PM 0
Share

when material = new $$anonymous$$aterial(renderer.shared$$anonymous$$aterial);

a new material is created and assigned to the variable material. lets call the new material.. material A then at the next iteration a new material is created again lets call it material b so before material = material A but now its material = material b therefore material A is no longer in use thats why i thought it might be collected.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Better way of Animating linear movement than AnimationCurve.Linear()? 1 Answer

how does houses in the nature deal with garbage and other kinds of waste 1 Answer

UnloadAssets / ReloadBehaviors In Profiler During Scene Change 0 Answers

Enabling Particle System's Emission Results Huge Amount Of GC 0 Answers

Unity iOS controlling heap size growth 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