Thursday, May 4, 2017

unity - the countdown timer inside while loop not working



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();
//textCDT
time = Random.Range(4f, time);
TextMesh txt = FindObjectOfType();



//the Timer:
time -= Time.deltaTime;
coolDown = time.ToString("0");
txt.text = ""+coolDown;

}
}

edit: i forgot to tell that the timer is inside SpawnCars().



Answer




Finally! After 3 days, I found the solution:



  1. I created a new script

  2. attached it to the 3d Text


  3. Add code to change the text:


    time-= Time.deltatime; // CountingDown
    GetComponent().text = time.ToString("0"); // Show the countdown in the 3d text



No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...