- Home /
My timer script makes Unity crash...
I am creating a game with a racing component, and wanted to create a on-screen timer to limit the time in a level. However for now I just want to make the timer count down every one second.
This is my code:
var minute = 2; //Minute place on Timer (3):43 //
var second1 = 0; //1st place on Timer 3:(4)3 //
var second2 = 0; //2nd place on Timer 3:4(3) //
var IsActive = true;
while(IsActive) // Toggle Timer //
{
while (second2 >= 0) // Reducees second1 each second
{
second2 = second2 - 1;
yield WaitForSeconds(1);
GetComponent(TextMesh).text = minute+":"+second1+second2; // Refreshes timer every second
}
while (second2 == 0) //Reduces second2 every 10 seconds
{
second1 == second1 - 1;
second2 == 9;
}
while (second1 == 0) //Reduces minute every minute.
{
minute == minute - 1;
second1 == 5;
}
}
It appears to me that this would work, however when I run it Unity crashes. Also try to ignore the fact that I am using 3dtext and not GUI text.
Thank You for your help :D
Thanks :) That helped a lot, now however I keep getting this error "BCE0044: expecting :, found '='. (12,17)"
However my line 12 (which looks like this second2 = second2 - 1;
)
does not have a '=' at the specified location...
Thank You :D
I am not absolutely certain about this, because I can't see the updated version of it. But in the original script you posted you were using "==" in several places that I think you meant to put "=".
You probably know this, but "==" is used when checking a value against another, and "=" is used to set a value to another.
Also, sometimes the character location doesn't seem to match up with actual error spot. If that's not creating the problem, put up the new code and I'll look at it.
God bless!
Howey
O$$anonymous$$ I tried that. I am still getting the same error.
Here is the updated script:
var $$anonymous$$ute ; //$$anonymous$$inute place on Timer (3):43 // var second1 ; //1st place on Timer 3:(4)3 // var second2 ; //2nd place on Timer 3:4(3) // var IsActive = true;
while(IsActive) // Toggle Timer // {
if (second2 >= 0); // Reducees second1 each second
{
second2 = second2 - 1;
yield WaitForSeconds(1);
GetComponent(Text$$anonymous$$esh).text = $$anonymous$$ute+":"+second1+second2; // Refreshes timer every second
}
if (second2 = 0) //Reduces second2 every 10 seconds
{
second1 = second1 - 1;
second2 = 9;
}
if (second1 = 0) //Reduces $$anonymous$$ute every $$anonymous$$ute.
{
$$anonymous$$ute = $$anonymous$$ute - 1;
second1 = 5;
}
Thank You :D
Ok, I think I found the problem. I put it into monodevelop and kept getting an error in the script. The problem is here:
if (second2 >= 0);
Remove the semicolon and it should work fine. Also I am not sure if this affects anything negatively, but in "while(isActive) //Toggle Timer // {" the bracket is also commented out.
If this solves your problem, I will change my first comment to an answer so you set it as the answer and then other people can see it as well.
God bless!
Howey
TY for your help, I got it to work now.
Thanks for spending so much time on this.
God bless you too. :D
Answer by Howey-Do-It · Apr 20, 2013 at 10:54 PM
I am not a pro with Java so I could be wrong here. I believe "while" is what is causing the crash. I have used "while" before and it caused the same issue for me.
Again I could be totally off-base here but I think if you put it in the Update() function and used "if" instead of "while" it would work.
I hope that helps or at least puts you on the right track.
God bless!
Howey
Your answer
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
Is it possible to set a format for a playerprefs value? (Timer) 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
My timer freezes everytime I leave the scene and come back. 1 Answer