using UnityEngine;
using System.Collections;

public class Grid : MonoBehaviour {
	public GameObject mySphere;
	private MeshRenderer myMeshRenderer;

	void Start() {
		Mesh mesh = GetComponent<MeshFilter>().mesh;
		Vector3[] vertices = mesh.vertices;
		GameObject mainSpheresParent = new GameObject ();
		mainSpheresParent.transform.position = new Vector3 (0,0,0);
		mainSpheresParent.tag = "Main Spheres";
		GameObject springSpheresParent = new GameObject ();
		springSpheresParent.transform.position = new Vector3 (0,0,0);
		springSpheresParent.tag = "Spring Spheres";
		Vector3 upaxis = new Vector3 (0, 1.0f, 0);

		foreach (Vector3 vert in vertices) {
			GameObject mainSpheres = Instantiate (mySphere,vert + transform.position,Quaternion.identity,mainSpheresParent.transform);
			GameObject springSpheres = Instantiate (mySphere,vert + transform.position,Quaternion.identity,springSpheresParent.transform);
			SpringJoint springs = mainSpheres.GetComponent<SpringJoint> ();
			Destroy (springSpheres.GetComponent<MeshRenderer> ());
			Destroy (springSpheres.GetComponent<MeshFilter> ());
			Rigidbody rgdbody;
			rgdbody = springSpheres.GetComponent<Rigidbody> ();
			springs.anchor = new Vector3 (0, -5.0f, 0);
			springs.axis = upaxis;
			springs.connectedBody = rgdbody;
		}
		//		Destroy (gameObject, 0.0f);
		//		mySphere.GetComponent<MeshRenderer>().enabled = false;
	}



}