WHY !! CountDown not Working inside the IF Conndition !!!
Hi guys i need a way to make the countdown decreasing inside the if statement i am stacking in this problem for more than 3 days .. Don't suggest to me Coroutine cause it will not working in my case cause i am using clones (initiated prefabs) .
i know that the countdown will work only one frame if it's inside an IF Statement ... Any Ideas to make it working ...Ty
The game is about cars ... and every instantiated car has a random countdown timer
void Update()
{
if(carNumber < cars)
{
SpawnCars();
}}
and this is the spawn function
void SpawnCars()
{
randomSpawnPoint = Random.Range(0, spawnPoints.Length);
randomSpawnCars = Random.Range(0, Cars.Length);
GameObject obj = Instantiate(Cars[randomSpawnCars], spawnPoints[randomSpawnPoint].position, Quaternion.Euler(-90, 180, 0)) as GameObject;
carNumber++;
obj.gameObject.tag = "select";// + k;
rcf = obj.GetComponent<RayCastForward>();
TextMesh txt = FindObjectOfType<TextMesh>();
time = Random.Range(4, 20);
txt.text = (time - Time.deltaTime).ToString("0");
print(time);
print(carNumber);
}
Answer by tormentoarmagedoom · Jan 16, 2019 at 11:47 AM
Good day,
I saw your comment.
First, If you are using Time.DetlaTime, which definition is:
The completion time in seconds since the last frame (Read Only).
This means, is the time between the last frame and the actual frame. (a very little number, like 0.000157)
So, If you dont use it inside the Update, it will not work.
second, you are not changing the value of variable "time", you are just substrating this little amout of time from the same number.
I mean, imagine the random gets a 5
You are doing this all time:
CanvasText = 5-time.deltatime
You need to do something like this
At the Start, or somewhere that only happens once:
float time= Random.Range[0,5]
At the Update, to calculate it each frame (Its a countdown, must be couting all time!)
time-= Time.DeltaTime;
Which is a short way to write this:
time= time- Time.DeltaTime;
so timeis changing every frame to a little one value
Bye!
no wait ... it not randomized every frame ... its randomized every instantiate prefab car (each time a clone appears have his own random timer) .. i try it in the game and every car takes a different random time ... the problem that the time is not counting_down its only run in one frame ! and the SpawnCar() i can control how many times will run with ( if(carNumber < cars))
Answer by salsa-project · Jan 16, 2019 at 08:12 PM
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 inn the 3d text
Your answer
Follow this Question
Related Questions
Countdown timer... 2 Answers
time related difficulty increase 0 Answers
how to link URL into multiple buttons in a condition "if" 1 Answer
How save playerprefs timer 0 Answers