Mesh.Destroy(myGameObject.GetComponent<MeshFilter>());
May it cause any problems or undefined behaviour? (the reason I ask this is that I'm trying to destroy an exiting component which is returned from function.
Mesh.Destroy(myGameObject.GetComponent<MeshFilter>());
May it cause any problems or undefined behaviour? (the reason I ask this is that I'm trying to destroy an exiting component which is returned from function.
You're not destroying the mesh. You're using the Destroy
method of the Mesh
class, which is inherited from the Object
class. Then you're calling that on the MeshFilter
component of the game object. This doesn't destroy the mesh, it's going to destroy the MeshFilter
component from your game object. Destroying in this way will delay the destroy until after the update loop, so, unless you're referencing the meshfilter yourself somewhere else, you won't have any problems.
It's OK to destroy a component in this way, but you can make it more clear with:
Destroy(myGameObject.GetComponent<MeshFilter>());
If you want to destroy the mesh, you'd do something like:
Destroy(myGameObject.GetComponent<MeshFilter>().mesh);
Which should destroy the instantiated mesh for that mesh filter. You'll have to do some testing around this, but I'm fairly sure Unity can handle its mesh being destroyed while still in use by the mesh filter.