Mesh Renderer not rendering (520202)

I’m loading in meshes at runtime, which works fine, but if I try to move the mesh to another object’s renderer, it won’t show up. If I don’t destroy tempObj, it sits at 0,0,0 with the model showing. The mesh shows up in this object’s Mesh Filter Mesh slot, but it just doesn’t show up in the world. It’s the same issue if I copy it between any gameObjects, but it’s fine if I instantiate a new object for it. I’ve tried this approach in a blank scene (with the path typed exactly) but it made no difference. Any help would be appreciated.

    GameObject tempObj = Instantiate(Resources.Load(loadStr) as GameObject) as GameObject;
    tempObj.AddComponent<MeshFilter>();
    tempObj.AddComponent<MeshRenderer>();
     
    if(!this.transform.GetComponent<MeshFilter>()) this.gameObject.AddComponent<MeshFilter>();
    if(!this.transform.GetComponent<MeshRenderer>()) this.gameObject.AddComponent<MeshRenderer>();
     
    this.transform.GetComponent<MeshFilter>().mesh = tempObj.GetComponent<MeshFilter>().mesh;
    this.transform.GetComponent<MeshRenderer>().material = tempObj.GetComponent<MeshRenderer>().material;
     
    Destroy( tempObj );

Anybody have any ideas as to what the issue could be?

Read your code man.

Instantiate tempobj; Add a mesh filter and renderer to it.
Add mesh filter and renderer to ‘this’
Get the mesh and material from tempobj

Problem is, you never assign a mesh and material to tempobj (tempobj.meshfilter.mesh = …)
if tempobj already has a meshfilter + mesh + renderer component, do not add new ones, just Get them

If I don’t add the MeshFilter and MeshRenderer to tempObj, I get an error saying “Object reference not set to an instance of an object” when I try to set ‘this’ filter and renderer to ‘tempObj’ filter and renderer. If I try making an empty GameObject first, giving it a renderer and filter, then loading in the mesh, it still gives me the error.

I don’t understand the code.

  1. You create a temporary game object.
  2. Add mesh filter and mesh renderer to it.
  3. Then if “this” object doesn’t have the these components, you also add them to “this” object.
  4. You copy mesh and material from the temporary object to “this” object. But the temporary object has no mesh and material assigned yet.
  5. Then you destroy the temporary object.

How does this object, that you load from resources, look like? What is it?

It’s either .fbx or .obj. The temporary object does have a mesh and material, because it shows up in the world with the model and mat visible. After I copy it to “this”, the model’s name appears in the Mesh Renderer box, but nothing appears in the view.

All I’m trying to do is load a mesh and give it to an existing gameObject. I haven’t been able to find a concise answer by searching; even Eric5h5 just said “instantiate as a gameObject” without much more elaboration in another thread.

I just sidestepped this issue by instantiating a new GO with the mesh, then parenting it to ‘this’.