- Home /
the countdown timer inside a spawn() and inside while loop not working
Hello Guys
i am instantiating prefab cars ... the clones had a random countdown timer with each one off them and its working very well but the its not decreasing its not counting down the timer is inside the spawn method the spawn method is inside the WHILE loop and the while loop is inside the UpDate() ... i tried to move the countdown to Update() but didnn't worked ... any ideas ...Thank You!
void Update()
{
p -= Time.deltaTime;
while (carNumber < cars && p <= 0 )
{
SpawnCars();
print(carNumber);
p = 5;
}
}
and this is the spawn method:
public void SpawnCars()
{
if (spawnAllowed)
{
// choose a random strat point 1/6
randomSpawnPoint = Random.Range(0, spawnPoints.Length);
// choose a random car 1/6
randomSpawnCars = Random.Range(0, Cars.Length);
// clone randomly a car(1/6) from a random pre_set-points(1/6)
GameObject obj = Instantiate(Cars[randomSpawnCars], spawnPoints[randomSpawnPoint].position, Quaternion.Euler(-90, 180, 0)) as GameObject;
// carNumber will count how many cloned car
carNumber++;
// destroy the cloned car after N sec
// Destroy(obj, 8);
// every cloned car will take a tag with it
obj.gameObject.tag = "select" + k;
k++;
rcf = obj.GetComponent<RayCastForward>();
//textCDT
time = Random.Range(4f, time);
TextMesh txt = FindObjectOfType<TextMesh>();
// CDT();
time -= Time.deltaTime;
coolDown = time.ToString("0");
txt.text = ""+coolDown;
}
}
Answer by sh_code · Jan 13, 2019 at 08:58 PM
My psychic debugging powers are telling me, that: 1. you don't need the if(spawnAllowed) condition, because in update you're only calling the spawn function when the timer already ticked down. 2. if I'm wrong, then try to ask the question in a way that actually makes sense and provides enough information to know what is going on.
Your answer
Follow this Question
Related Questions
Timers independant of the game i.e set timers 3 Answers
How to integrate timer into my C# script? 2 Answers
Timer doesn't count 0 Answers
How to reduce object materials ? 1 Answer
Multiple Cars not working 1 Answer