- Home /
 
 
               Question by 
               AnnoyedShepherd12 · Feb 01 at 06:17 PM · 
                c#transform.positiongeometry  
              
 
              Create spheres on the vertices of a Cube
I'm quite new to Unity here and I have been trying to draw a couple of spheres on the vertex of a Cube gameobject. The code is as follows
 public GameObject Elements;
  public float gapWidth = 0.01f;
 
 void Start()
 {Elements = GameObject.CreatePrimitive(PrimitiveType.Cube);
         Elements.name = "Ele1";
         Elements.transform.position = new Vector3(0.0f * gapWidth + 0.5f, 0.0f * gapWidth + 0.5f, 0.0f * gapWidth + 0.5f);
         Elements.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
 Vector3[] vertices = Elements.GetComponent<MeshFilter>().mesh.vertices;
 
 GameObject[] Spheres = new GameObject[vertics.Length];
         for (int i = 0; i < vertices.Length; i++)
         {
             Spheres[i] = GameObject.CreatePrimitive(PrimitiveType.Sphere);
             Spheres[i].transform.position = vertices[i];
             Spheres[i].transform.localScale -= new Vector3(0.8F, 0.8F, 0.8F);
         }
 }
 
               The problem here is that the spheres occur somewhere else in world space and not on the vertices of the Cube. I think I am going wrong somewhere in transform.position. Any help would be appreciated.
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by Hellium · Feb 01 at 06:45 PM
You need to convert the vertices spaces from the object's space to the world space
         Spheres[i].transform.position = Elements.transform.TransformPoint(vertices[i]);
 
              Your answer