instantiating things at vertices points.

Hi all.

I’m looking to make a function that makes an array out of all the vertices in a gameobject, and when called, Instantiates a prefab at each of the vertices, whilst destroying the gameobect.
I’m talking turned-into-sand sort of idea.
I know my code finds the verts and puts them into an array fine, since I’ve bastardised my own code from wave making script. For now, I’m just Instantiating simple spheres at the vert points.
When I call the function, the gameobject disappears as planned, and the clones are made, in the right shape (one per vertices), but they’re grouped around world 0,0,0.
I’ve even tried making each vert in the For loop a child of the main gameObject, and re-positioning it, in case it was a parenting/relative transform issue, but it’s the same.
I know there must be a simple answer, but I can’t see it for the life of me.

Here’s the code:

var mf1 = GetComponent(MeshFilter);
var vertices1 : Vector3[]  = mf1.mesh.vertices;

for(var i : int = 0; i < vertices1.Length; i++)
{
var crm = Instantiate(crumb, vertices1*, transform.rotation);*

crm.transform.parent = gameObject.transform; //added when I ran out of ideas!

crm.position = vertices1*; // same again…*

}
(edit “crumb” is my Transform exposed variable)
Can anyone else help me please? I’ll post screenshots if I’ve not explained myself properly and anyone wants to see.
Many thanks.
quick edit
Once the “crumbs” have been created, if I pause the game, and un-parent them, they are indeed clustered around the world zero.
Here’s some screenshots:
[37027-ssprob1.jpg|37027]_
_
[37028-ssprob2.jpg*|37028]*
_*
_*

That’s just using the location of the vertices in the mesh; if you want the world coords of the transform you can do something like (untested):

var matrix = transform.localToWorldMatrix;
for (var i = 0; i < vertices1.Length; i++) {
    var crm = Instantiate (crumb, matrix.MultiplyPoint3x4 (vertices*), transform.rotation);*

What you are attempting is pretty heavy on the CPU side of things you should perhaps reconsider either using a vertex shader for the effect or using a Particle emitter for the effect.

That being said vertices are stored within the mesh in local coordinate space. Since you pass them directly to Instantiate they will all be copied directly and as you’ve realized be grouped around the world origin. You need to use Transform.TransformPoint to transform them to WorldSpace before you instantiate the objects. Alternatively you need to parent it to the mf1 object and not an arbitrary Game Object then simply switch off the mesh renderer for mf1. Your mesh will dissapear and all your objects should now be grouped around the mf1’s origin. Or move your GameObject to mf1’s position.

Translate to WorldSpace example:

var mf1 = GetComponent(MeshFilter);
var vertices1 : Vector3[]  = mf1.mesh.vertices;
 
 for(var i : int = 0; i < vertices1.Length; i++)
 {
	 var Go = mf1.gameObject;
	 var crm = Instantiate(crumb, Go.TransformPoint(vertices1*), transform.rotation);*
  • crm.transform.parent = gameObject.transform; //added when I ran out of ideas!*

_ crm.position = vertices1*; // same again…_
_
}*_
Correct Parenting example:
var mf1 = GetComponent(MeshFilter);
var vertices1 : Vector3[] = mf1.mesh.vertices;

for(var i : int = 0; i < vertices1.Length; i++)
{
* var Go = mf1.gameObject;*
_ var crm = Instantiate(crumb, vertices1*), transform.rotation);
crm.transform.parent = Go; //changed to parent the correct transform*_

}
//lastly disable the objects MeshRenderer / Skinned Mesh Renderer. Goto wherever you are
//currently destroying the object and delete the destruction code.
// get an instance of the MeshRenderer and set its active property to false;