Loading a Mesh at run time - possible bug?

So here’s what I’m doing:

  • Prefab that has a Mesh Renderer and Mesh Filter component attached - Mesh Filter has no Mesh
  • Using Resource.Load() I load another GO that has the Mesh that I need
  • I add that Mesh as the ().mesh of the Prefab
  • I continue to add various Components based on user selection - blah blah blah

Everything appears to work ok the first time but then the GO that is Resource.Load’ed loses it’s Mesh target in the Editor and I have to re-save from 3DSMax in order for it to come back.

Is this a bug or something I am missing associated with Resource.Load?

When you load a prefab with Resource.Load(), anything you do to that object is saved to disk as a permanent change to the file. So you could end up drastically changing that file.

To avoid this, you need to make a new instance of the loaded object and make all changes to that.

I’m not exactly sure what you are doing, no posted code, hopefully this fixes things for you.

Indeed I should have posted - apologies:

public GameObject guyTemplate;  // basic shell GO

void spawnGuy() {
    buildGuy(guyTemplate);
    Network.Instantiate(guytemplate, Vector3.zero, Quaternion.identity, 0);
}

void buildGuy(GameObject newGuy) {
    newGuy.name = "someName";
    GameObject tempGO = Resources.Load("specificMesh") as GameObject;
    Mesh tempMesh = tempGo.GetComponent<MeshFilter>().mesh;
    newGuy.GetComponent<MeshFilter>().mesh = tempMesh;
}

Looking in the Inspector - when everything is loaded the Mesh Filter of “specificMesh” changes from meshName to meshName Instance and my only guess is that the Network.DestroyPlayerObjects is destroying it upon exiting.

What Tzan said–instead of Resources.Load…, do Instantiate(Resources.Load…

–Eric

Yup - was just about to come back here and say that it worked. :slight_smile:

Another quick follow-up as I was poking around today. Is there an easy way to add a GameObject as the child of another GameObject at run time?

Set transform.parent.

–Eric