- Home /
Problem is not reproducible or outdated
Combine mesh instance N is null, (but why?)
I've seen occasional "Combine mesh instance 0 is null" warnings before, but this script is giving tons of them each time it runs.
(......combine mesh instance 1263 is null, combine mesh instance 1266 is null, combine mesh instance 1269 is null, combine mesh instance 1272 is null...)
It's only one every three, for whatever reason.
I don't understand what's wrong here, or how to fix it, and why I'm not getting any NullReferenceException errors if those warnings really mean what they say.
And other than generating over a thousand warnings, the function works just fine- it IS currently combining all the plants into one mesh, and all their shadows into another mesh.
the offending code:
function Flatten () {
var meshFilters = GetComponentsInChildren.<MeshFilter>() as MeshFilter[]; //find all meshes
var shadowMFs : List.<MeshFilter> = new List.<MeshFilter>();
var solidMFs : List.<MeshFilter> = new List.<MeshFilter>();
for (var pmf : MeshFilter in meshFilters) //sort all meshes into shadows and not-shadows
{
if (pmf.gameObject.layer == 27) { shadowMFs.Add (pmf); }
else { solidMFs.Add (pmf); }
}
var combineShadows : CombineInstance[] = new CombineInstance [shadowMFs.Count];
for (var i = 0; i < shadowMFs.Count; i++) //build a CombineInstance for all the shadows
{
combineShadows[i].mesh = shadowMFs[i].sharedMesh;
combineShadows[i].transform = shadowMFs[i].gameObject.transform.localToWorldMatrix;
shadowMFs[i].gameObject.SetActive (false);
}
Debug.Log ("ShadowMFs :" +i);
transform.Find("Shadows").GetComponent (MeshFilter).mesh = new Mesh ();
transform.Find("Shadows").GetComponent (MeshFilter).mesh.CombineMeshes (combineShadows); //combine them
transform.Find("Shadows").gameObject.SetActive (true);
var combinePlants : CombineInstance[] = new CombineInstance [solidMFs.Count];
for (var j = 0; j < solidMFs.Count; j++) // build a CombineInstance for all the solid objects
{
combinePlants[j].mesh = solidMFs[j].sharedMesh;
combinePlants[j].transform = solidMFs[j].gameObject.transform.localToWorldMatrix;
solidMFs[j].gameObject.SetActive (false);
}
Debug.Log ("SolidMFs :" +j);
transform.GetComponent (MeshFilter).mesh = new Mesh ();
transform.GetComponent (MeshFilter).mesh.CombineMeshes (combinePlants); // /!\ THE WARNING IS CAUSED HERE /!\
transform.gameObject.SetActive (true);
renderer.sharedMaterial = solidMat;
for (var childT : Transform in myTransform as Transform) // clean up empty prefabs
{
if (childT.gameObject.name == "Shadows") {
childT.gameObject.GetComponent.<Renderer>().sharedMaterial = shadowMat;
} else {
Destroy (childT.gameObject, 0.1);
}
}
yield;
}
Thanks! But turns out it was the prefabs. Derp. They had some empty mesh filters I didn't know about (and the clover stems had mesh colliders by accident.) Fixing the prefabs got rid of all but two errors, and adding the line
if (!pmf.mesh) {continue;}
in the first for loop got rid of those too.
Answer by Starwalker · Nov 13, 2013 at 07:04 PM
You'll need to put a Debug.Log of the List.Count value before and after each of the type of combine operations, I'm guessing that when you hit the play, the List have no mesh to combine in that particular frame or the mesh's its supposed to combine are not instantiated already, and when you do a list operation on an un-initialized list, the errors will show up.
Again this would be the 1st step I would take to check why nulls happen. The other solution is to put an if() statement before each loop, something like: if(solidMFS) { do list operation }
or if(solidMFS.Count >= 0) { do list operation }
, this is done so that the loop only executes if the list is initialized and not on a null list. And this is also a solution if the 1st debug test lead to null values for a few frames. If any List operation is done which is List element dependent (on a null valued element), it will break the whole flow if that script.
Hope it helps.