private bool AllDead()
 {
     foreach (GameObject c in cars)
     {
         CarManager2 m = c.GetComponent(typeof(CarManager2)) as CarManager2;
         if (m.isEnabled())
         {
             return(false);
         }
     }
     return(true);
 }
    // Update is called once per frame
    void Update()
    {
        //The boxes should jump if the sphere is cose to origo
        //			if (AllDead())
        //			{

        for (int i = 0; i < car_inst; i++)
        {
            CarManager2 m = cars[i].GetComponent(typeof(CarManager2)) as CarManager2;
            //					int ind = rng.Next (0, Positions.Length);
            //					m.Restart (Positions[ind],Rotations[ind]);
            double d = rng.NextDouble();
            if (!m.isEnabled())
            {
                m.Restart(Positions[i], d > 0.5 ? Rotations[i] : Alt_Rotations[i]);
            }
        }
        //			}
        episode = EpisodeController.getEpisode();
    }
    //	public static Vector3[] MPositions = {new Vector3 (-2.8f, 0.1f, 1.3f), new Vector3 (-2.8f, 0.1f, -0.29f)};
    //	public static Quaternion[] MRotations = { Quaternion.AngleAxis(270, new Vector3(0, 1, 0)), Quaternion.AngleAxis(270, new Vector3(0, 1, 0))};

    // Use this for initialization
    void Start()
    {
        //new NNTest ();
        Application.runInBackground = true;
        cars     = new GameObject[numcars];
        Qnetwork = new MLP(state_size, hiddenSizes);
//		obstacles = new GameObject[numobstacles];
        QtargetNetwork = new MLP(state_size, hiddenSizes);
        int i = 0;
//		for (int i = 0; i < numcars; i++){
        //			int ind = rng.Next (0, Positions.Length);
        //			cars[i] = (GameObject) Instantiate(CarFab,Positions[ind],Rotations[ind]);
        double d = rng.NextDouble();

        cars[i] = (GameObject)Instantiate(CarFab, Positions[i], d > 0.5 ? Rotations[i] : Alt_Rotations[i]);
        CarManager2 m = cars[i].GetComponent(typeof(CarManager2)) as CarManager2;

        m.SetMLP(this.Qnetwork, this.QtargetNetwork);
        cars[i].name = "CloneCar" + i;
//		}
        EpisodeController = cars[0].GetComponent(typeof(CarManager2)) as CarManager2;
    }
    private GUIStyle guiStyle = new GUIStyle();     //create a new variable
    void OnGUI()
    {
        guiStyle.fontSize = 48;         //change the font size
        GUI.Label(new Rect(10, 10, 100, 200), "Episode: " + episode, guiStyle);
        if (GUI.Button(new Rect(10, 100, 50, 30), "Save"))
        {
            Debug.Log("SAVED");
            this.QtargetNetwork.write("QTargetNetwork");
            this.Qnetwork.write("QNetwork");
        }

        if (GUI.Button(new Rect(10, 140, 50, 30), "Load"))
        {
            Debug.Log("LOADED");
            this.QtargetNetwork.read("QTargetNetwork");
            this.Qnetwork.read("QNetwork");

            for (int i = 0; i < car_inst; i++)
            {
                CarManager2 m = cars[i].GetComponent(typeof(CarManager2)) as CarManager2;
                m.epsilon = m.epsilon_min;
                Debug.Log("Epsilon set to: " + m.epsilon);
            }

            //EpisodeController.epsilon = 0.1f;
            //EpisodeController.epsilon_min = 0.1f;
        }

        if (GUI.Button(new Rect(10, 180, 70, 30), "Obstacle"))
        {
            Debug.Log("OBSTACLE");
            int index = rng.Next(0, numobstacles);
//			obstacles[index] = (GameObject) Instantiate(Obstacle,OPositions[index],ORotations[index]);
        }

        if (GUI.Button(new Rect(10, 220, 70, 30), "Add Car"))
        {
//			int index = rng.Next (0, numobstacles);
//			obstacles[index] = (GameObject) Instantiate(Obstacle,OPositions[index],ORotations[index]);

            //set the epsilon to the existing cars epsilon

            if (car_inst < numcars)
            {
                Debug.Log("Car added.");
                double d = rng.NextDouble();
                cars[car_inst] = (GameObject)Instantiate(CarFab, Positions[car_inst], d > 0.5 ? Rotations[car_inst] : Alt_Rotations[car_inst]);
                CarManager2 m = cars[car_inst].GetComponent(typeof(CarManager2)) as CarManager2;
                m.epsilon = m.epsilon_min;
                m.SetMLP(this.Qnetwork, this.QtargetNetwork);
                cars[car_inst].name = "CloneCar" + car_inst;
                car_inst++;
            }
        }

        if (GUI.Button(new Rect(10, 260, 70, 30), "Restart"))
        {
            Debug.Log("RESTART");
            for (int i = 0; i < car_inst; i++)
            {
                CarManager2 m = cars[i].GetComponent(typeof(CarManager2)) as CarManager2;
                m.Die();
                double d = rng.NextDouble();
                if (!m.isEnabled())
                {
                    m.Restart(Positions[i], Rotations[i]);
                }
            }
        }

        if (GUI.Button(new Rect(10, 300, 70, 30), "Test Mode"))
        {
            Debug.Log("Test mode");
            for (int i = 0; i < car_inst; i++)
            {
                CarManager2 m = cars[i].GetComponent(typeof(CarManager2)) as CarManager2;
                m.test_mode = !m.test_mode;
                Debug.Log("Test mode: " + m.test_mode);
            }
        }
    }