// Use this for initialization void Start() { float width = gameObject.GetComponent <RectTransform> ().rect.width; float height = gameObject.GetComponent <RectTransform> ().rect.height; CarController.imWidth = width; CarController.imHeight = height; string[] files = Directory.GetFiles("positions/", "*.txt", SearchOption.TopDirectoryOnly); nr_cars = files.Length; cars = new CarController[nr_cars]; List <Thread> threads = new List <Thread> (); CarController.defaultToSend = new bool[nr_cars]; int i = 0; foreach (string name in files) { GameObject newCar = Instantiate(prefab) as GameObject; CarController controller = newCar.GetComponent <CarController> (); cars [i] = controller; CarController.defaultToSend [i] = true; // Can't be multithreaded due to instantiates controller.Begin(name, i); // Multithreaded Socket connections to python Thread t = new Thread(delegate() { controller.SetupSockets(); }); threads.Add(t); t.Start(); i++; } foreach (Thread t in threads) { t.Join(); } foreach (CarController c in cars) { c.StartReceiving(); } // Setup was finished for all cars, ready to get working CarController.working = true; }