0
\$\begingroup\$

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>();


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

    }
}

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

\$\endgroup\$
5
  • \$\begingroup\$ this while loop will loop at most once, since you set p = 5 and have the condition p <= 0. Shouldn't you initialize with p = 5; and inside the loop do p -= Time.deltaTime; and change the condition to p >= 0? \$\endgroup\$ Jan 13 '19 at 16:07
  • \$\begingroup\$ pis a spawn timer ... 1 car every 5 sec ... and carsis how many car will be instantiated ... and this loop is looping correctly ..if i put 'cars = 4' it will give 4 cars (a car every 5sec) \$\endgroup\$
    – Mr Salsa
    Jan 13 '19 at 16:14
  • \$\begingroup\$ Then it looks more like it should be an if, because after setting p = 5 the loop will terminate after first loop because of the condition p <= 0. \$\endgroup\$ Jan 13 '19 at 16:26
  • \$\begingroup\$ The countdown only runs once each time you spawn a car, you need to put it in 'update' somewhere. \$\endgroup\$
    – JeffUK
    Jan 14 '19 at 10:21
  • \$\begingroup\$ Additionally, your time variable inside Cars isn't a timer. p is your timer. \$\endgroup\$ Jan 14 '19 at 15:49
1
\$\begingroup\$

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<TextMesh>().text = time.ToString("0"); // Show the countdown in the 3d text
    
\$\endgroup\$
0
\$\begingroup\$

You could use a Coroutine. You can run SpawnCars(); there and use yield return new WaitForSeconds(5f); to make it run every 5 seconds. You have to put StartCoroutine("YourCoroutine"); in Start(); and when you don't need anymore cars you will have to call StopCoroutine("YourCoroutine");.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.