How to remove items in a list based on if the player object is not in a collider object that has the same assigned number variable as the list item

I’m still having problems. I have a system in which there are triggers with an assigned height variable. When the player enters one of these triggers, it’s value is added to a list, which is then sorted and the highest value is assigned to the player. However I need to ensure that when the player exits one of these triggers, the list item corresponding to the exited colliders number should be removed.


Here is the code for entry:

void OnTriggerEnter2D (Collider2D ground)
	{
		GroundHeightComponent = ground.GetComponent<GroundLvlAssign>().SetGroundLvl;
		if (ground.gameObject.GetComponent<GroundLvlAssign>().SetGroundLvl != m_PlayerStatistics.m_PlayerGroundLvl)
		{			
			m_PlayerStatistics.m_PreviousPlayerGroundLvl = m_PlayerStatistics.m_PlayerGroundLvl;
			m_PlayerStatistics.m_PlayerGroundLvl = HighestGroundHeightComponent;
		}
}

Staying:

void OnTriggerStay2D (Collider2D ground)
	{	
		//If ground is not the same set Previous ground level to player ground level and then set current ground level to floor ground level
		if(GroundHeightComponent != null && !GroundCollidersInside.Contains(GroundHeightComponent))
		{
			GroundCollidersInside.Add(GroundHeightComponent);
			GroundCollidersInside.Sort();
			HighestGroundHeightComponent = GroundCollidersInside[GroundCollidersInside.Count - 1];
		}
}

Exiting (Not working as intended):

	void OnTriggerExit2D(Collider2D ground)
	{
		//check through all of the numbers colliders inside list to check for if they matchthe collider you exited's number snd then remove them
		for (int i=0;i<GroundCollidersInside.Count;++i)
		{
			if(GroundCollidersInside *== ground.gameObject.GetComponent<GroundLvlAssign>().SetGroundLvl && GroundCollidersInside.Contains(i))*
  •  	{*
    
  •  		GroundCollidersInside.Remove(i);*
    
  •  		GroundCollidersInside.Sort();*
    
  •  		HighestGroundHeightComponent = GroundCollidersInside[GroundCollidersInside.Count - 1];*
    
  •  	}*
    
  •  }*
    
  •  if (ground.gameObject.GetComponent<GroundLvlAssign>().SetGroundLvl != m_PlayerStatistics.m_PlayerGroundLvl)*
    
  •  {			*
    
  •  	m_PlayerStatistics.m_PlayerGroundLvl = HighestGroundHeightComponent;*
    
  •  }*
    
  • }*
    I have a good Idea why the exit code doesn’t work as intended, but what I don’t know is how to make sure that on exit of a trigger, that the list item that matches the exited trigger’s height value is removed.

Nevermind I came up with a solution. It’s definitely not perfect.
Essentially In the ontriggerexit I cleared the list and then moved the ontriggerexit to before the ontriggerstay.


Like this:

	void OnTriggerExit2D(Collider2D ground)
	{
		//check through all of the numbers colliders inside list to check for if they matchthe collider you exited's number snd then remove them
		for (int i=0;i<GroundCollidersInside.Count;++i)
		{
			if(GroundCollidersInside *== ground.gameObject.GetComponent<GroundLvlAssign>().SetGroundLvl && GroundCollidersInside.Contains(i))*
  •  	{*
    
  •  		GroundCollidersInside.Clear();*
    
  •  		HighestGroundHeightComponent = 1;*
    
  •  	}*
    
  •  }*
    
  • }*

  • void OnTriggerStay2D (Collider2D ground)*

  • { *

  •  //If ground is not the same set Previous ground level to player ground level and then set current ground level to floor ground level*
    
  •  GroundHeightComponent = ground.GetComponent<GroundLvlAssign>().SetGroundLvl;*
    
  •  if(GroundHeightComponent != null && !GroundCollidersInside.Contains(GroundHeightComponent))*
    
  •  {*
    
  •  	GroundCollidersInside.Add(GroundHeightComponent);*
    
  •  	GroundCollidersInside.Sort();*
    
  •  	HighestGroundHeightComponent = GroundCollidersInside[GroundCollidersInside.Count - 1];*
    
  •  }*
    
  • }*
    Though the flaw with this is this GroundHeightComponent = ground.GetComponent().SetGroundLvl; in ontriggerstay. That’s horrible and inefficient.