Array index is out of range in MeshMorpher

I want to change a mesh to another in Unity, both have the same vertices, but i get this error in runtime:

IndexOutOfRangeException: Array index is out of range.
mesh2.SetComplexMorph (Int32 srcIndex, Int32 dstIndex, Single t) (at Assets/prueba/mesh2.js:22)
mesh2.SetMorph (Single t) (at Assets/prueba/mesh2.js:40)
mesh2.Update () (at Assets/prueba/mesh2.js:86)

And I have this code, which I attached to a first mesh:

var m_Meshes : Mesh[]; 
var m_AnimateAutomatically = true; 
var m_OneLoopLength : float = 1.0;   /// The time it takes for one loop to complete 
var m_WrapMode = WrapMode.Loop; 
private var m_AutomaticTime : float = 0; 
 
private var m_SrcMesh : int = -1; 
private var m_DstMesh : int = -1; 
private var m_Weight : float = -1; 
private var m_Mesh: Mesh; 
 
/// Set the current morph in 
function SetComplexMorph (srcIndex : int, dstIndex : int, t : float) { 
   if (m_SrcMesh == srcIndex && m_DstMesh == dstIndex && Mathf.Approximately(m_Weight, t)) 
      return; 
   var v0 = m_Meshes[srcIndex].vertices; 
   var v1 = m_Meshes[dstIndex].vertices; 
   var vdst = new Vector3[m_Mesh.vertexCount]; 
   for (i=0; i<vdst.Length; i++) 
      vdst <em>= Vector3.Lerp(v0_, v1*, t);*_</em> 

m_Mesh.vertices = vdst;
m_Mesh.RecalculateBounds();
}

/// t is between 0 and m_Meshes.Length - 1.
/// 0 means the first mesh, m_Meshes.Length - 1 means the last mesh.
/// 0.5 means half of the first mesh and half of the second mesh.
function SetMorph (t : float) {
var floor : int = t;
floor = Mathf.Clamp(floor, 0, m_Meshes.Length - 2);
var fraction : float = t - floor;
fraction = Mathf.Clamp(t - floor, 0.0, 1.0);
SetComplexMorph (floor, floor + 1, fraction);
}

function Awake () {
enabled = m_AnimateAutomatically;
var filter : MeshFilter = GetComponent(MeshFilter);
// Make sure all meshes are assigned!
for (i=0; i<m_Meshes.Length; i++) {
if (m_Meshes == null) {
Debug.Log(“MeshMorpher mesh " + i + " has not been setup correctly”);
m_AnimateAutomatically = false;
return;
}
}

// At least two meshes
if (m_Meshes.Length < 2) {
Debug.Log (“The mesh morpher needs at least 2 source meshes”);
m_AnimateAutomatically = false;
return;
}

filter.sharedMesh = m_Meshes[0];
m_Mesh = filter.mesh;
var vertexCount = m_Mesh.vertexCount;
for (i=0; i<m_Meshes.Length; i++) {
if (m_Meshes*.vertexCount != vertexCount) {
Debug.Log(“Mesh " + i + " doesn’t have the same number of vertices as the first mesh”);
m_AnimateAutomatically = false;
return;
_}
}
}*_

function Update () {
if (m_AnimateAutomatically) {
var deltaTime = Time.deltaTime * (m_Meshes.Length - 1) / m_OneLoopLength;
m_AutomaticTime += deltaTime;
var time : float;
if (m_WrapMode == WrapMode.Loop)
time = Mathf.Repeat(m_AutomaticTime, m_Meshes.Length - 1);
else if (m_WrapMode == WrapMode.PingPong)
time = Mathf.PingPong(m_AutomaticTime, m_Meshes.Length - 1);
else
time = Mathf.Clamp(m_AutomaticTime, 0, m_Meshes.Length - 1);

SetMorph (time);
}
}

@script RequireComponent (MeshFilter)
Can anyone help me please? Thank you very much.

…umh…so let me get this straight: You start Play mode with one model, and then you enable “Animate automatically”? This would be proof you didn’t write this script, because then you would know that this doesn’t make sense. Next time, please mention this, because people will consider different possibilities as answers, and won’t have to waste time trying to figure out where the error in the script is, when in fact there is none…

  • The initialization in Awake() already checks all required conditions necessary to morph. So the idea is, “Animate automatically” must be enabled before you press Play, and if any problems in your scene setup would prevent the morph from working, the script will disable this flag, and you can’t expect it to work by re-enabling it during Play mode…
  • Besides, between which models is Unity supposed to morph if you only have one?

But now I have other problems, when I run the game in Unity, the mesh changes its position and it is deformed. Can there be any problems when exported from 3DS Max?

Yes, but this is an unrelated problem. Please search Unity Answers about that, you will find many explanations and possible causes for this already.

EDIT: Hm, you say, the error happens when “Animate automatically” is enabled, but you don’t manually enable it after you pressed Play? I don’t see any possible way for this to happen in that script. If so, please provide a snapshot of the situation where the error occurs (or perhaps another video?)

Ok you are right.
The script works fine, and the error was mine, I was applying the incorrect meshes,so the problem is solved.

Thank you very much.