- Home /
Problem with countdown timer with texture using an array
I'm working on a countdown timer with textures. I've looked at some of the other questions, but still can't find the a solution on what i'm trying to do. Right now, Here is my current code:
var num0 : Texture;
var num1 : Texture;
var num2 : Texture;
var num3 : Texture;
var num4 : Texture;
var num5 : Texture;
var num6 : Texture;
var num7 : Texture;
var num8 : Texture;
var num9 : Texture;
public var timeNum : int[];
timeNum = [time2, time];
private var countdown : boolean;
private var currentNumber : Texture;
private var currentNumber2 : Texture;
private var time : int;
private var time2 : int;
private var newNumber = [currentNumber2, currentNumber];
function Start() {
countdown = true;
Countdown();
}
function Countdown () {
if (countdown) {
while (true) {
time -= 1;
yield WaitForSeconds(1);
if(time == 9){
currentNumber = num9;
}
if(time == 8){
currentNumber = num8;
}
if(time == 7){
currentNumber = num7;
}
if(time == 6){
currentNumber = num6;
}
if(time == 5){
currentNumber = num5;
}
if(time == 4){
currentNumber = num4;
}
if(time == 3){
currentNumber = num3;
}
if(time == 2){
currentNumber = num2;
}
if(time == 1){
currentNumber = num1;
}
if(time == 0){
currentNumber = num0;
}
if(time2 == 9){
currentNumber2 = num9;
}
if(time2 == 8){
currentNumber2 = num8;
}
if(time2 == 7){
currentNumber2 = num7;
}
if(time2 == 6){
currentNumber2 = num6;
}
if(time2 == 5){
currentNumber2 = num5;
}
if(time2 == 4){
currentNumber2 = num4;
}
if(time2 == 3){
currentNumber2 = num3;
}
if(time2 == 2){
currentNumber2 = num2;
}
if(time2 == 1){
currentNumber2 = num1;
}
if(time2 == 0){
currentNumber2 = num0;
}
if(time == -1){
if(time2 >= 1){
time2 -=1;
time = 9;
currentNumber = num9;
}
if(time2 <= 0){
countdown = false;
}
}
}
}
}
function OnGUI () {
if(countdown){
GUI.Box ( Rect (15, 325, 400, 200),currentNumber2);
GUI.Box ( Rect (30, 325, 400, 200),currentNumber);
}
}
What I want to do is make it so that I can set the time in the inspector which then will assign the correct number texture based on the number entered. Then in the GUI.Box will countdown showing the number texture.
Right now there are no errors, but, when it runs it only displays the number 1. and nothing else..
Any help available?
Answer by TSorbera · Mar 14, 2012 at 05:44 PM
The whole way you're doing it is very roundabout and hard to read. Also, (especially at low frame rates) using WaitForSeconds(1) will give you inaccurate results, since it basically waits for that much time to pass and then for the next frame to be drawn. Try something like this code here and see if it'll work better for you. nums
indices 0 through 9 should be the digits 0 through 9. When you set countdownTarget
, it should be, e.g. countdownTarget = Time.time + 5;
for 5 seconds in the future.
var nums : Texture[];
var countdownTarget;
private var countdown : boolean;
function Start() {
countdown = true;
}
function OnGUI () {
if(countdown){
var timeRemaining = Mathf.RoundToInt(countdownTarget - Time.time);
if (timeRemaining <= 0)
{
countdown = false;
return;
}
var left = 0.0;
var timeRemainingStr = timeRemaining.ToString();
for (var i = 0; i <= timeRemainingStr.Length; i++)
{
left += 15;
GUI.Box(Rect(left, 325, 400, 200), nums[int.Parse(timeRemainingStr.Substring(i, 1))]);
}
}
}
Thanks for the assist.... There is one problem that I keep seeing and can't get around with the code you gave me..
In this line:
GUI.Box ( Rect ((left == left + 15), 325, 400, 200),nums[int.Parse(timeRemainingStr.Substring(i, 1))]);
I get this error: BCE0024: The type 'UnityEngine.Rect' does not have a visible constructor that matches the argument list '(boolean, int, int, int)'.
I tried several changes and just ended up with more errors...
That should be left = left + 15
, not left == left + 15
. This is similar to ++left
, except that I want to increment it by 15 ins$$anonymous$$d of 1. I'll edit my code so it's clearer by putting those on separate lines. Also, for the future, that probably should have been a comment on my answer, not a new answer of your own.
sorry first time using the unity answers didn't even see the comment button till right now lol :)
use a switch or if/else is at least. At the moment, your program checks ever single statement one by one even though the first one is right. That wastes a lot of resources.