Memory Leak in Build Run

Hey guys i am working on a 2D Game with different Rooms and I create the rooms randomly.
So far everything worked just as I planned! But when I Build Run and look into my Task-Manager my Memory gets more and more MB! (not in unity when i play)

I disabled some of the code and found out that it has something to do with this script anyone knows why? I Always use the same material.

//Door open or closed:
public bool b_Open = true;
	
private bool b_Player1	= false;
private bool b_Player2  = false;
	
//Door Materials
public Material Open_mat;
public Material Closed_mat;
	
void Awake () 
	{
		//set the value to false when initializate
		b_Open = true;
		renderer.enabled = false;
	}
	
public void Update () 
	{
	//change texture from open to closed and the other way around:
	if(b_Open == true  renderer.material != Open_mat)
			renderer.material = Open_mat;	
	else if(b_Open == false  renderer.material != Closed_mat) 
			renderer.material = Closed_mat;			
	}

Is there no one who knows something? Or is this to easy? I am new to this please help i tried Google but did not find anything to memory leak with materials like this one above.

Your renderer.material may create a new material. Have a closer look at the docs: Unity - Scripting API: Renderer.material

I didn’t check it in detail, but that may be the issue. Instead of renderer.material != Open_mat or Closed_mat, you may have a boolean which knows if the current material is the open or closed one.

Or even better, don’t use Update at all and have a function to switch between open and close.

Thanks i try this out! I hope that this material things wont leak in every situations because i made a upperclass for my charakter and do animations with different materials :confused:

//in underclass of Enemy uses enemyAnimation function
public Material[] NormalAnimation;
//upperclass
protected void enemyAnimation(Material[] m_Array, float Animationspeed)
	{
	
		//Timer of the Animation:
		f_AnimTimer += Time.deltaTime; // Keep track of passing time
		if (f_AnimTimer < Animationspeed) 
		{
			//do nothing		
		}
		else 	
		{
			//change Material
			i_MatCount = m_Array.Length-1;
			for(int i= 0; i<i_MatCount; i++)
			{
				if(m_Array[i_MatCount] == m_Array[i]  i < (i_MatCount-1))
					{
						m_Array[i_MatCount] = m_Array[i+1];
						f_AnimTimer = 0;
						break;
					}
				else if (i >= (i_MatCount-1))
					{
						m_Array[i_MatCount] = m_Array[0];
						f_AnimTimer = 0;
					}
			}
		}
		renderer.material = m_Array[i_MatCount];
	}

Thanks for the reply Dantus!

Read the docs!

What do you mean with

I mean I have to check to at runtime: if the door may opened or not? Wouldn’t a function be called in the update as well?

Thanks! I deleted the:

like you said:

And my memory was rather stable thanks I think i have to fix some different things as well didn’t think that unity creates a instance for material comparisons!