Waiting for mesh to be created, CombineChildren

In code I’m creating an empty GO, adding prefabs and the CombineChildren script. Then I want to use the combined mesh that is created on a Mesh Collider.


Error Message:
MissingComponentException: There is no ‘MeshFilter’ attached to the “terrain1” game object, but a script is trying to access it.
You probably need to add a MeshFilter to the game object “terrain1”. Or your script needs to check if the component is attached before using it.

The mesh object exists. You can see it in the inspector.

While running I can add the combined mesh object to the collider manually in the inspector. This works fine, but need it to work in code.

EditElev is a script that raises and lowers the mesh when its clicked on. Its how I know the collider is working, or not.

		// created empty GO terrain1, 
                //added script CC, 
                //added prefabs to be children of terrain1


		terrain1.active = true;	//runs attached scripts like CombineChildren
				    

              terrain1.AddComponent(typeof(MeshCollider));
		MeshCollider mc = (MeshCollider)terrain1.GetComponent(typeof(MeshCollider));
		MeshFilter	mf = (MeshFilter)terrain1.GetComponent(typeof(MeshFilter));
		
		
		mc.sharedMesh = mf.sharedMesh;   // <---------bad line--------
		
				terrain1.AddComponent("EditElev");

After some more work, this is happening because the process for making the combined mesh is “probably” seperate from this code and isnt finished before I call for the mesh.

I set the code after the .active command to start on a button click and it worked.

So I need a way to wait for the mesh to be created before I call it. I tried some looping but it was infinite.

I tried putting a yield in it but I get that “expecting ;” error.

I’ve spent 3 hours on this today, woohoo!

Are you making sure that you are updating the reference to the “new combined” GameObject?

At first I wasnt, but changed it to this, which is the latest I have.

I create and set mf to null before this.

do
		{
			
			mf = (MeshFilter)terrain1.GetComponent(typeof(MeshFilter));
			if(mf != null)
		   {
				mc.sharedMesh = mf.sharedMesh;
			   done = true;
			   Debug.Log ("mesh set");
		   }
		  
			
		}while(!done);

Setting this to a button click and using the same terrain1 reference works fine.

So I figured I needed to keep setting MeshFilter until its not null.

Ok, so you think it is that you access the reference before the Combine Children script is finished.

I do know that you can access that combine children script (correct me if im wrong please)

Why dont you modify it and create a flag that let you know when the combine children has done and then activate your trigger based on the flag you created?

Correct

I just went for a 15 min walk before your first answer and I was thinking the same thing, adjusting the Combine Children script. Exactly what form that takes I havent figured out yet.

That will be a project for tomorrow I think. :slight_smile:

Thanks!

No worries :wink:

please let us know how it goes! :smile:

Well I set a flag in CombineChildren. Set it to true on the last line of Start().

In the main script in Update() I checked if it was true.

Then in the funtion that assigned the mesh I set another flag so its only called once.

So that worked.
Unity didnt like that do/while loop at all.

void Update () {
		
		if(cc_script.meshDone  doOnce)
		{
			doTerrain();
		}
}

This is fine for hacking around.

I suspect that I may be doing that main GO with one Update() entry point. So I can register/unregister classes for updates. Having a hundred updates() attached to GOs all doing nothing 99.9% of the time is silly.

We’ll see, I’m still in hacking around learning mode :slight_smile:

Great to know it worked!!! :smile: