Changing Mesh of an object depending on Health Value. C#

I’m trying to set up code that will cause the mesh of the player to change depending on how much health they have. I believe that I’m supposed to use MeshFilter.mesh but I’m not completely sure how to do so.

Code I’m using:

public class PlayerHealth_Test : MonoBehaviour {
	
public float CurrentHealth = 100.0f;
public GameObject Player;
private float MaxHealth = 100.0f;
public bool MeshChange = false;
public Mesh NoDam;
public Mesh LowDam;
public Mesh MedDam;
public Mesh HighDam;
	
void Start()
	{
		Player = GameObject.FindGameObjectWithTag("Player");
	}

	
	void Update () {
	if (CurrentHealth > 100)
		{
			CurrentHealth = MaxHealth;
		}
	if (MeshChange == true);
		{
		if (CurrentHealth == 100.0f)
			{
				Mesh = GetComponent<MeshFilter>(NoDam).mesh;
			}
		
		if (CurrentHealth <= 75.0f)
			{
				Mesh = GetComponent<MeshFilter>(LowDam).mesh;
			}
		if (CurrentHealth <= 50.0f)
			{
				Mesh = GetComponent<MeshFilter>(MedDam).mesh;
			}
		if (CurrentHealth <= 25.0f)
			{
				Mesh = GetComponent<MeshFilter>(HighDam).mesh;
			}
		
		}
	if (CurrentHealth <= 0)
		{
		GameObject.DestroyObject(Player);
		}
	
	}
}

The only errors right now are in the mesh assigning lines of the if statements because, again, I don’t really know how to use MeshFilter.mesh. I’ve looked through the script reference but looking over the example code doesn’t really give me a really clear idea on how to use it.

Sometimes it’s easier to switch GameObjects altogether, as described here.

Past that, you’re probably looking for something more like this:

if (MeshChange) {
    Mesh myMesh;
    if (CurrentHealth > 75f) {
        myMesh = NoDam;
    } else if (CurrentHealth > 50f) {
        myMesh = LowDam;
    } else if (CurrentHealth > 25f) {
        myMesh = MedDam;
    } else {
        myMesh = HighDam;
    }
    GetComponent<MeshFilter>().mesh = myMesh
}

Keep careful track of your data flow: if you’re picking one Mesh from a set, and then assigning it to a MeshFilter, your code should read like you’re doing just that.

Watch your function calls: there’s no version of GetComponent() that takes arguments past the type requested.

In this case I would probably create 4 textures (or MORE) to represent the health left and swap those ( GUI elements ) according the damage / healing received by the player. And if it looks too bad, make more textures and change them with higher frequency.