Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by fingaz1 · Mar 14, 2012 at 05:10 PM · arrays

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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))]);
         }
     }
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fingaz1 · Mar 15, 2012 at 08:47 PM 0
Share

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

avatar image TSorbera · Mar 15, 2012 at 10:19 PM 0
Share

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.

avatar image fingaz1 · Mar 16, 2012 at 06:57 AM 0
Share

sorry first time using the unity answers didn't even see the comment button till right now lol :)

avatar image fafase · Mar 16, 2012 at 07:08 AM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to set color to gameObject respective to below coding? 2 Answers

Ways to minimize Object.stelemref() calls (low level) 1 Answer

JesseEtzler's RPG Talent System in C# 1 Answer

Dungeon crawler problem(getting transform data from an array) 1 Answer

Array index is out of range 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges