Runtime Mesh Manipulation With Unity | raywenderlich.com

One of the benefits of using Unity as your game development platform is its powerful 3D engine. In this tutorial, you’ll get an introduction to the world of 3D objects and mesh manipulation.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3169311-runtime-mesh-manipulation-with-unity

Hi, I tried to clone a mesh (the very start of the tutorial) and get an error: Assets\RW\Scripts\MeshStudy.cs(76,9): error CS0103: The name ‘meshFilter’ does not exist in the current context. As far as I understand - some names need to be changed in the code.

@shogan Can you please help with this when you get a chance? Thank you - much appreciated! :]

Hi @brand17,

So sorry I missed your question a few weeks back!

I’ve had a look and found that there is a problem with the variable names in the starter project. It looks like somewhere in our edit pipeline one of us forgot to update variable names. If you compare the starter project and the code you entered from the tutorial with the final project files you’ll see the differences.

But here is the fix, relevant up to the point where you got to before seeing that error in the console.

At the top the of the MeshStudy.cs script, make sure the first variable declarations are:

Mesh originalMesh;
Mesh clonedMesh;
MeshFilter oMeshFilter;

Then, in the InitMesh() method, change it to look like this:

public void InitMesh()
{
    oMeshFilter = GetComponent<MeshFilter>();
    originalMesh = oMeshFilter.sharedMesh; //1
    clonedMesh = new Mesh(); //2

    clonedMesh.name = "clone";
    clonedMesh.vertices = originalMesh.vertices;
    clonedMesh.triangles = originalMesh.triangles;
    clonedMesh.normals = originalMesh.normals;
    clonedMesh.uv = originalMesh.uv;
    oMeshFilter.mesh = clonedMesh;  //3

    vertices = clonedMesh.vertices; //4
    triangles = clonedMesh.triangles;
    isCloned = true; //5
    Debug.Log("Init & Cloned");
}

Save the file and return to the Unity editor. That should get you back on track. If the tutorial adds more code to this class further along, be sure to use the new variable names as per above.

Sorry again for the delay in response, and thanks @shogunkaramazov for the bump :]

I’ll speak to the team about getting the instructions here updated.

1 Like

Hi,
Thanks for the tutorial. The thing is I am combing multiple meshes but i get a weird artifact where vertices are joined in. I found through Internet that it is due to Lighting calculations and we have to recalculate tangent over the whole mesh again. But when i recalculated tangents the light is not as smooth as it should be on the stitching points. Can you please expand upon on calculation of tangents and normal in this or a supplement tutorial with example of stitching two or more meshes together.

Thanks.

You also need to change the last two lines of EditMesh() to:
clonedMesh.vertices = vertices;
clonedMesh.RecalculateNormals ();

@shogan Sorry if all the edits are generating a lot of emails…

Just downloaded today - Wed Dec 18 2019. Was really enjoying the lesson and learning many things I wanted to learn about. However, needed to so a lot of sleuthing because these var still have not been fixed and there are a few other var issues across the lesson.

I am up to the Heart lesson:

HeartMeshInspector.cs
No overload for method ‘SaveMesh’ takes 1 arguments [Assembly-CSharp-Editor] [136, 35]

HeartMesh.cs
The name ‘meshToSave’ does not exist in the current context [Assembly-CSharp] [187-190,5]
The name ‘meshToSave’ does not exist in the current context [Assembly-CSharp] [191,12]

  • In the FINAL there is an additional Mesh nMesh = new Mesh(); line.
  • In STARTER meshToSave. In FINAL nMesh.

Also different in HeartInspector.cs

  • In STARTER prefabMesh. In FINAL pfMesh
  • In STARTER prefabToInstantiate. In FINAL pfObj
  • In STARTER referencePrefab. In FINAL pfRef

With all of this fixed and no errors, in Runtime the Mesh is NOT saved to the Prefab. The following section of OnInspecterGUI has different logic that is not changed in the lesson. When changed to FINAL it works.

@shogan Do you have any feedback about this? Thank you - much appreciated! :]

Hi kalmdown3d :]

Sorry that this reply is delayed and that you had further issues.

I took another look at this article and found other issues that were causing your troubles. I’ve gone through the starter and final projects and corrected things, then re-uploaded the Downloadable Materials to sort it all out.

That overload you pointed out was definitely faulty - it shouldn’t have been there on the SaveMesh method. Rather just SaveMesh(); So that is also fixed.

Further to that, there was a line missing from the instructions when saving the mesh (adding more code to the OnInspectorGUI() method to save the mesh). The line missing was: AssetDatabase.AddObjectToAsset(prefabMesh, path); (this has been added to the article instructions now too.

Hopefully that covers everything and everything should flow without errors now!

Thanks @shogunkaramazov for the heads up on the reply here that I missed a few weeks back.

Cheers
Sean

1 Like

This tutorial is more than six months old so questions are no longer supported at the moment for it. Thank you!