Change mesh of affected objects

Hi I have a basic explosion script that pushes all of the boxes around but I want all damaged objects mesh to be changed

Just to test if it is working I’m just using Cubes for now and trying to change any damages objects to Spheres

public float force;
public float radius;
public GameObject explosion;
Mesh box;

void Update (){
	if(Input.GetMouseButtonDown(0))
	{
		Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		RaycastHit hit;

		if(Physics.Raycast (ray, out hit, 70))
		{
			GameObject explo = Instantiate(explosion, hit.point, Quaternion.identity) as GameObject;
			Destroy (explo, 3);

			Collider[] colliders = Physics.OverlapSphere(hit.point, radius);

			foreach(Collider c in colliders)
			{
				if(c.rigidbody == null) continue;

				c.rigidbody.AddExplosionForce(force, hit.point, radius, .5f, ForceMode.Impulse);

				box = c.GetComponent<MeshFilter>().mesh;
					
					if(box == "Cube")
					{
						c.GetComponent<MeshFilter>().mesh = "Sphere";
					}
			}
		}
	}
}

}

I’m not quite sure on how to use MeshFilter

I think “box = c.GetComponent().mesh;” is correct but how would I call that in an IF statement and then instantiate it?

You’re mostly there!

Firstly, you should use MeshFilter.sharedMesh. This is much better for optimization.

Secondly, MeshFilter.mesh and MeshFilter.sharedMesh take a Mesh object rather than a string. In order to make your script work you will need a reference to a Mesh and assign that instead:

public Mesh cube;
public Mesh sphere;

...

Mesh current = c.GetComponent<MeshFilter>().sharedMesh;

if(current == Cube)
    c.GetComponent<MeshFilter>().sharedMesh = sphere;