- Home /
Change mesh to to GameObject
Hi guys! I'm using a script to place objects on a grid that I found made by the community (thank you!) and it has a mesh that is drawn purple on the grid where the mouse is positioned. Now I want the mesh to instead of being just a solid color I want it to be a GameObject. How would I modify this code to make it a GameObject instead?
private Mesh mesh;
private Vector3[] verts;
private Vector2[] uvs;
private int[] tris;
void ConstructMesh()
{
if ( !mesh )
{
mesh = new Mesh();
MeshFilter f = GetComponent("MeshFilter") as MeshFilter;
f.mesh = mesh;
mesh.name = gameObject.name + "Mesh";
}
mesh.Clear();
verts = new Vector3[9 * 9];
uvs = new Vector2[9 * 9];
tris = new int[ 8 * 2 * 8 * 3];
float uvStep = 1.0f / 8.0f;
int index = 0;
int triIndex = 0;
for ( int z = 0; z < 9; z ++ )
{
for ( int x = 0; x < 9; x ++ )
{
verts[ index ] = new Vector3( x, 0, z );
uvs[ index ] = new Vector2( ((float)x) * uvStep, ((float)z) * uvStep );
if ( x < 8 && z < 8 )
{
tris[ triIndex + 0 ] = index + 0;
tris[ triIndex + 1 ] = index + 9;
tris[ triIndex + 2 ] = index + 1;
tris[ triIndex + 3 ] = index + 1;
tris[ triIndex + 4 ] = index + 9;
tris[ triIndex + 5 ] = index + 10;
triIndex += 6;
}
index ++;
}
}
// - Build Mesh -
mesh.vertices = verts;
mesh.uv = uvs;
mesh.triangles = tris;
mesh.RecalculateBounds();
mesh.RecalculateNormals();
}
void UpdateMesh()
{
int index = 0;
for ( int z = 0; z < 9; z ++ )
{
for ( int x = 0; x < 9; x ++ )
{
verts[ index ] = mapGrid[ x, z ];
index ++;
}
}
// assign to mesh
mesh.vertices = verts;
mesh.RecalculateBounds();
mesh.RecalculateNormals();
Thanks!
Answer by Hoeloe · Oct 11, 2013 at 11:47 PM
Your question makes no sense. Let's just start with a simple fact about Unity:
If it is in the scene, it is necessarily a GameObject.
When you instantiate a mesh, you are not really creating a "mesh", as such, but you are creating a GameObject with a MeshFilter and MeshRenderer attached to it. The reason it's displaying purple is because what you haven't done is attached a material to the renderer, so it has no information as to what the surface should look like. With no information, to prevent a crash, Unity just uses a fallback, which is to draw everything as solid purple. Attach a material to your instantiated MeshRenderer, and then you'll find it works okay.
Thank you for your answer, but how do I assign a material? I've tried $$anonymous$$aterial mat = Resources.Load("Gravel0031_4_S") as $$anonymous$$aterial; renderer.material = mat;
but that doesn't work...
What is the resource "Grave10031_4_s"? It sounds to me like that's not a material, and the "as" cast you're doing is then setting it to null.
Your answer
Follow this Question
Related Questions
GUI Buttons and game objects? 1 Answer
Dynamic mesh positioning with Raycast intersection... 0 Answers
How can I save a gameobject's mesh? 1 Answer
Click on object to activate another object 3 Answers
Building asset bundles and wanting to move game objects from one parent to a newly created parent 1 Answer