void Update() { if (Time.time > nextSpawn) { float chance = Random.value; if (chance < spawnChance) { GameObject car = Instantiate(carPrefab); car.transform.position = transform.position; Material color = colors[Random.Range(0, colors.Length)]; foreach (Transform child in car.transform) { Renderer rend = child.gameObject.GetComponent <Renderer>(); if (child.name == "Body") { rend.material = color; } } CarAI ai = car.GetComponent <CarAI>(); ai.SetWayPoints(wayPoints); } nextSpawn = Time.time + rate * Random.value; } }