# I want this to update a mesh collider everytime i use it.

When i use this script, it does everything i want EXECPT i only updates the mesh collider one time, id like it to happen everytime i deform the mesh.

var pull = 10.0;
private var unappliedMesh : MeshFilter;

``````enum FallOff { Gauss, Linear, Needle }
var fallOff = FallOff.Gauss;

static function LinearFalloff (distance : float , inRadius : float) {
return Mathf.Clamp01(1.0 - distance / inRadius);
}

static function GaussFalloff (distance : float , inRadius : float) {
return Mathf.Clamp01 (Mathf.Pow (360.0, -Mathf.Pow (distance / inRadius, 2.5) - 0.01));
}

function NeedleFalloff (dist : float, inRadius : float)
{
}

function DeformMesh (mesh : Mesh, position : Vector3, power : float, inRadius : float)
{
var vertices = mesh.vertices;
var normals = mesh.normals;

// Calculate averaged normal of all surrounding vertices
var averageNormal = Vector3.zero;
for (var i=0;i<vertices.length;i++)
{
var sqrMagnitude = (vertices *- position).sqrMagnitude;*
``````
• `````` // Early out if too far away*
``````
• `````` if (sqrMagnitude > sqrRadius)*
``````
• `````` 	continue;*
``````
• `````` var distance = Mathf.Sqrt(sqrMagnitude);*
``````
• `````` var falloff = LinearFalloff(distance, inRadius);*
``````

_ averageNormal += falloff * normals*;_
_
}_
_
averageNormal = averageNormal.normalized;*_

* // Deform vertices along averaged normal*
* for (i=0;i<vertices.length;i++)*
* {*
_ sqrMagnitude = (vertices - position).sqrMagnitude;
* // Early out if too far away*
continue;*_

* distance = Mathf.Sqrt(sqrMagnitude);*
* switch (fallOff)*
* {*
* case FallOff.Gauss:*
* break;*
* case FallOff.Needle:*
* break;*
* default:*
* break;*
* }*

vertices += averageNormal * falloff * power;
* }*

* mesh.vertices = vertices;*
* mesh.RecalculateNormals();*
* mesh.RecalculateBounds();*
}

function Update () {

* // When no button is pressed we update the mesh collider*
* if (!Input.GetMouseButton (1))*
* {*
* // Apply collision mesh when we let go of button*
* ApplyMeshCollider();*
* return;*
* }*

* // Did we hit the surface?*
* var hit : RaycastHit;*
* var ray = Camera.main.ScreenPointToRay(Input.mousePosition);*
* if (Physics.Raycast (ray, hit))*
* {*
* var filter : MeshFilter = hit.collider.GetComponent(MeshFilter);*
* if (filter)*
* {*
* if (filter != unappliedMesh)*
* {*
* ApplyMeshCollider();*
* unappliedMesh = filter;*
* }*

* // Deform mesh*
* var relativePoint = filter.transform.InverseTransformPoint(hit.point);*
_ DeformMesh(filter.mesh, relativePoint, pull * Time.deltaTime, radius);
* }
}
}*_

function ApplyMeshCollider () {
* if (unappliedMesh && unappliedMesh.GetComponent(MeshCollider)) {*
* unappliedMesh.GetComponent(MeshCollider).mesh = unappliedMesh.mesh;*
* }*
* unappliedMesh = null;*
}

I think you need to use:

``````unappliedMesh.GetComponent(MeshCollider).sharedMesh
``````

http://docs.unity3d.com/Documentation/ScriptReference/MeshCollider-sharedMesh.html