- Home /
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[i] = Vector3.Lerp(v0[i], v1[i], t);
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[i] == 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[i].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.
At the start of your function SetComplex$$anonymous$$orph, try
m_$$anonymous$$esh.Clear();
we need to see the code passing to the setcomplexmorph.
basically what its saying is m_$$anonymous$$eshes[x] isn't working because m_$$anonymous$$eshes size is smaller than x.
Thats a bit interesting because I can see there you have a script trying to ensure the size of m_$$anonymous$$eshes is greater than or equal to 2.
insert
debug.log("begin log") debug.log(srcIndex); debug.log(dstIndex); debug.log(m_$$anonymous$$eshes.length); debug.log("end log")
in the setcomplexmorph function to see if its the problem of passing a bad source index, a bad destination index or the m_$$anonymous$$eshes array itself is bad.
I don't see any severe errors in your code. When exactly does this error occur, simply after pressing Play, or are you modifying the component and its parameters while the game is running? If the latter, what exactly are you modifying? Or does the problem occur in a build ins$$anonymous$$d of the Editor? How large is your m_$$anonymous$$eshes array in the Inspector?
alucardj: I have tried m_$$anonymous$$esh.Clear(); but the error continues. In my script, the error only appears when the "Animate automatically" box is checked. Update: and when I select only one mesh.
sparkzbarca: Ok, if I select two meshes the error desappear. In the log I get the follow:
begin log
0
1
2
end log
Wolfram: The error occured only when I run the game, without change any parameter
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 $$anonymous$$ax?
I leave you a video so you see the problem. Thanks for all. http://www.youtube.com/watch?v=$$anonymous$$8$$anonymous$$3yjmP7lc
Answer by rober3287 · Nov 14, 2012 at 07:49 PM
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.
Answer by Wolfram · Nov 14, 2012 at 10:48 AM
...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?)
Sorry, because I didn't say that I got the script from http://wiki.unity3d.com/index.php/$$anonymous$$esh$$anonymous$$orpher
But if you see the video, you can check as when I start Play mode, the "Animate automatically" is activated already.
About the models, I have three meshes to morph with the same vertices.
We cannot find the source to your problem if you keep changing the premises and don't give clear feedback to the questions we ask. So please explain the exact situation when and where this error occurs:
Do you have one, two or three models assigned? You say "not 2", you seem to use 3, but your log output is from a configuration of 2.
What is the actual length of your array (the number shown in the "Size" parameter)?
"Animate automatically" is enabled, stays enabled after you press Play, and you don't modify it? According to the script, it is impossible for the error to occur in this case...
Are you certain this is a "live" error, and not some old console error from a previous run where you modified some parameters?
I have one model in the Scene, and two more assigned in the Inspector, three meshes in total. In the script I have to put at least two meshes.
The "Size" paremeter shows 2.
Yes, I enable the "Animate automatically" before I press Play and it is not modified when is running.
Now I have no errors, because the cause was that I put only a mesh assigned, and I had to assign at least two.
But my mesh now is deformed like in the video.
Thanks for your answer.
If your array index problem is solved (or at least no longer an issue for you), this question should be marked as answered, to prevent others from reading through the whole lengthy discussion and code, just to realize it's no longer relevant.
Since there currently is no suitable answer, in this case you should just post an answer to your own question (stating something like "Problem solved, I was using only one mesh"), and mark that as "accepted as correct" (=tick the checkmark).
(However, I still insist your problem cannot have happened through regular use of the script, even with only one mesh assigned, since the script catches these cases. So "I was using only one mesh" cannot have been the (only) reason that you received this error)
About your other problem - as I mentioned before, it is a different problem, so it doesn't belong in this question (this is not a forum, it's a topic-specific knowledge base), and you should search the site for an already given answer or similar question, or if necessary post your deformation/transformation problem as a new question.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Get all vertices in gameObject to array. 1 Answer
Enemy Moving Does Not Work With A Model? 1 Answer
Replace MeshFilter mesh by a other mesh in Editor 1 Answer
Simple mesh height alteration. 1 Answer