How would you show a Mesh before instantiating it?

I want a user to position an object in the scene.

So I have a prefab “myprefab” and draw the mesh like this:

Graphics.DrawMesh ( myprefab.GetComponent().mesh, matrix, material,0);

But often it doesn’t get drawn and after I stop the game the mesh in the prefab goes missing.

I must be doing something wrong. Is it wrong to use a mesh in a prefab? Another thing I wanted to do was copy a mesh in a prefab into a game object in the scene. This also seems to make the prefab mesh go missing when I stop the game. I’m unsure what is happening.

I am using Unity 4.7

Edit: I see this is marked as a bug fixed in Unity 5.0. Any workaround for Unity 4.7 maybe?

You know, there’s a reason in the docs it says obsolete in red letters. If you want to render a mesh, use a meshrenderer

The doc says some versions of DrawMesh are obsolete not all of them.

Thanks, I’ll have a look at MeshRenderer but I don’t see how that solves the problem of referencing a mesh from a prefab.

You can have a preview object in the scene, with a MeshRenderer component attached, and via script you can change that MeshRenderer’s mesh to whatever mesh you want to display.

Yeah but when I try and set the preview object mesh to the mesh in the prefab all kinds of things go wrong. I think it is a bug in Unity 4 though.

Someone else had the same problem here: http://answers.unity3d.com/questions/46580/how-to-render-a-non-instantiated-prefab-on-the-sce.html

It appears to be OK if I use get the sharedMesh instead of mesh from the prefab. So I think that solves it.

What I do is simply make a new prefab that has the same mesh but a different shader and script components.

When you try to place it a script on the preview does some validation, spawns in the actual object, and then destroys/pools itself

1 Like