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!

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.