Skip to main content
Post Closed as "Duplicate" by Draco18s no longer trusts SE, CommunityBot
added 100 characters in body
Source Link

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

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

    }
}

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

Source Link

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

    }
}