Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Unity automatically creates new materials but does not destroy themThanks

Discussion in 'General Graphics' started by nsmith1024, Dec 15, 2016.

  1. nsmith1024

    nsmith1024

    Joined:
    Mar 18, 2014
    Posts:
    869
    Hello,

    I have some big memory leaks because my Unity app has a lot of the same prefabs all over the scene, and I have to assign a different texture to all of them.

    So when I do this

    prefab.GetComponent<Renderer>().material.mainTexture = newTexture

    I read that unity creates a new material and assigns it the new texture in order to make that texture assigned only to that prefab, but it doesn't delete the old material so it creates memory leaks until my program crashes.

    Since unity creates a new material and assigns it the new texture, how do i remove the old material? How do I even access it in order to destroy it? When I destroy it does it automatically destroy the old texture as well, or do i have to destroy that manually also?

    Thanks
     
  2. andrewgotow

    andrewgotow

    Joined:
    Dec 28, 2013
    Posts:
    18
    Object.Destroy (and by extension, DestroyImmediate) can be called on anything derived from UnityEngine.Object, including Materials.

    You can manually clean up the materials created by assigning a new texture by making a call to Destroy with the old material first.

    ---

    However, if you're using a more modern version of Unity, I would highly recommend looking into "Material Property Blocks", which allow you to assign per-renderer properties to materials without creating duplicates.
     
    theANMATOR2b likes this.
  3. nsmith1024

    nsmith1024

    Joined:
    Mar 18, 2014
    Posts:
    869
    Hello andrew,

    Thanks for your comment, I have many identical prefabs all over the scene, and im constantly downloading new textures from a server and assigning them to parts of the prefabs, each of them must have a different texture so it they cant use the same material.

    I've been having this problem for months, and tried many different things but it always runs out of memory after a little while of use. Sometimes it just starts acting strange for example some operations in the game just stops working, while others keeps working, very strange.

    Im going absolutely crazy getting frustrated with it, today has been the worst since the memory leaks are the only thing left to fix, but its kicking my butt, feel like to quit. I just have a small scene and the profiler tells me im using 700 meg just in textures. How do other people do it becuase i seen games with huge scenes and i have this little scene that the profiler claims consumes 700MB.
     
  4. andrewgotow

    andrewgotow

    Joined:
    Dec 28, 2013
    Posts:
    18
    Ahh, so that doesn't sound like an issue with materials being duplicated, but the textures themselves!

    As long as you keep downloading textures, your system is going to keep allocating new memory. The only real solution is to Destroy the textures you don't need anymore. You'll need to keep track of the textures that you download and be sure to destroy them with

    Object.Destroy( myTexture );

    when you no longer need them.

    Unfortunately a general solution for something like this is difficult because you may need to keep certain textures around depending on how your project works. If all of your textures are downloaded off the internet and used only once, it should be safe to just destroy them when you replace them with a new texture.
     
    theANMATOR2b likes this.