Legacy Documentation: Version 5.5
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

MeshFilter.mesh

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Switch to Manual
public var mesh: Mesh;
public Mesh mesh;

Description

Returns the instantiated Mesh assigned to the mesh filter.

If no mesh is assigned to the mesh filter a new mesh will be created and assigned.

If a mesh is assigned to the mesh filter already, then first query of mesh property will create a duplicate of it, and this copy will be returned. Further queries of mesh property will return this duplicated mesh instance. Once mesh property is queried, link to the original shared mesh is lost and MeshFilter.sharedMesh property becomes an alias to mesh. If you want to avoid this automatic mesh duplication, use MeshFilter.sharedMesh instead.

By using mesh property you can modify the mesh for a single object only. The other objects that used the same mesh will not be modified.

It is your responsibility to destroy the automatically instantiated mesh when the game object is being destroyed. Resources.UnloadUnusedAssets also destroys the mesh but it is usually only called when loading a new level.

Consider mesh property as a shortcut for the following code:

#pragma strict
function Start() {
	var mesh: Mesh = GetComponent.<MeshFilter>().sharedMesh;
	var mesh2: Mesh = Instantiate(mesh);
	GetComponent.<MeshFilter>().sharedMesh = mesh2;
}
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { void Start() { Mesh mesh = GetComponent<MeshFilter>().sharedMesh; Mesh mesh2 = Instantiate(mesh); GetComponent<MeshFilter>().sharedMesh = mesh2; } }

Which is called on first query of mesh property.

Note:
If MeshFilter is a part of an asset object, quering mesh property is not allowed and only asset mesh can be assigned.

#pragma strict
// Distorts the mesh vertically.
function Update() {
	// Get instantiated mesh
	var mesh: Mesh = GetComponent.<MeshFilter>().mesh;
	// Randomly change vertices
	var vertices: Vector3[] = mesh.vertices;
	var p: int = 0;
	while ( p < vertices.Length ) {
		vertices[p] += new Vector3(0, Random.Range(-0.3F, 0.3F), 0);
		p++;
	}
	mesh.vertices = vertices;
	mesh.RecalculateNormals();
}
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { // Distorts the mesh vertically. void Update() { // Get instantiated mesh Mesh mesh = GetComponent<MeshFilter>().mesh; // Randomly change vertices Vector3[] vertices = mesh.vertices; int p = 0; while (p < vertices.Length) { vertices[p] += new Vector3(0, Random.Range(-0.3F, 0.3F), 0); p++; } mesh.vertices = vertices; mesh.RecalculateNormals(); } }

See Also: Mesh class.