BCE0022: Cannot convert 'UnityEngine.Mesh' to 'Mesh'.

Hi, I created a new project and just by adding an script with these lines

function Start () {
var mesh : Mesh = GetComponent(MeshFilter).mesh;
}

and i get the following error:

Assets/Mesh.js(2,48): BCE0022: Cannot convert ‘UnityEngine.Mesh’ to ‘Mesh’.

Any idea??

1 Answer

1

The problem is that your file is also called Mesh.js! Unity is getting confused between its inbuilt Mesh type, and your custom Mesh type. You need to rename your js file, so that there is no longer a conflict.

Or use UnityEngine.Mesh as type instead of Mesh but that's not the recommended solution ;) You can even bypass this error by let the compiler choose the type for you, thanks to <a href="http://en.wikipedia.org/wiki/Type_inference">type-inference</a>. (Type-inference has nothing to do with dynamic typing!): var mesh = GetComponent.<MeshFilter>().mesh; When you create a variable and assign something to it during the creation the compiler statically types your variable with the assigned type.