Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Mugs500 · Apr 21, 2016 at 04:50 AM · c#

Making a counter that does not reflect on time

Hi All,

I'm working on making a counter that does not count time or a "score in the game". I guess the best way to explain it as i want to count to 360 then back to 0, and continue counting. I have put a delay between each count so i can alter how fast it will count. I also have it updating a GUITEXT for debugging. I basically want to be able to trigger different events/animations at different time intervals.

I have tried a few options but most of the time unity will just crash. I have the function set up to a button to initiate for debug purposes.

Any help will be appreciated. Thanks

using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.EventSystems;

public class Counter : MonoBehaviour {

 public Text counter;
 private int count;
 public int delay = 2000;
 public Button startCount;

 // Use this for initialization


 void Start()
 {
     count = 0;
 }



 public void StartCount()
 {
     if (startCount == true)
     {
         Debug.Log("start count pressed");
         TimeCounter();
     }
 }


 void SetCountText()
 {
     counter.text = count.ToString();

 }
 void TimeCounter()
 {
     if (count != 360)
     {
         SetCountText();
         System.Threading.Thread.Sleep(delay);
         count = count + 1;
         TimeCounter();
     }


     if (count == 360)
     {
         System.Threading.Thread.Sleep(delay);
         count = 0;
         TimeCounter();
     }

 }

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Toon_Werawat · Apr 21, 2016 at 04:37 PM

So... You want to count to 360 every 2 seconds? Even Time.timeScale is 0? (When game pause)

Try this.

 using UnityEngine;
 using UnityEngine.UI;
 
 public class Counter : MonoBehaviour
 {
     public Text counter;
     public int count
     {
         get
         {
             return _c;
         }
         set
         {
             if (value == 360) //If it larger then 360.
             {
                 value = 0;
             }
             _c = value;
             //You can do another thing here. As a counter is increese
             counter.text = value.ToString(); //Everytime this variable is change. It will automatic update your **counter Text**
         }
     }
     private int _c; //Part of Auto-Property
     public int delay = 2;//Originally your delay is 2000 miliseconds. But this one use second instead of millisecond.
     public Button startCount;
     public bool isCounting { get; set; } //Instead of counting forever. Atlease make it can stop ._.
     private float lastTime; //Threading alternative
 
     void Start()
     {
         count = 0;
         startCount.onClick.AddListener(delegate { StartCount(); }); //In case if you not assign it at inspector. Remove this if you already assign in inspector
         lastTime = Time.time + delay; //Threading sleep alternative
     }
 
     public void StartCount()
     {
         isCounting = !isCounting;
         if (isCounting)
         {
             Debug.Log("Start count pressed");
         }
     }
 
     void Update()
     {
         if (isCounting)
         {
             if (Time.time > lastTime) //Time.time is always update by unity.
             {
                 //After delay reach
                 count += 1; //Count up. (As you originall want;
                 lastTime = Time.time + delay;
             }
         }
     }
 }
 

Comment
Add comment · Show 2 · 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 Mugs500 · Apr 22, 2016 at 01:25 AM 0
Share

Just tried it out. Changed the delay to of float and altered the delay, Exactly what I wanted. Thanks.

P.s $$anonymous$$y calculations were way off in the below entry lol

avatar image Mugs500 · Apr 27, 2016 at 08:25 AM 0
Share

@Toon_Werawat

Is there anyway to make this count faster, I set the interval to 0 and i still would like the option to count faster.

avatar image
0

Answer by Mugs500 · Apr 22, 2016 at 01:17 AM

@Toon_Werawat

Thanks for the help, it has got me closer to the goal. A few things I need to change.

I don't want to count to 360 then wait 2 seconds, I had a larger time limit there just for debugging purposes. What I'm trying to achieve is for example an an engine rotation = 360 degrees, hence why the count is to 360.

I want the pause between each count so I can essentially alter the speed of the motor, preferably milliseconds.

so if I want the speed to be 1 revolution per minute the pause between each count + 1 would be 0.0027777777777778. So if I calculated right it should count to 360 every minute, with no pause between.

Eventually I want create events at certain points of the count. I will try work with the code you gave as it works a lot better then my original. Any more tips would be greatly appreciated.

Thank You

Comment
Add comment · Show 1 · 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 Toon_Werawat · Apr 22, 2016 at 01:19 PM 0
Share

I have no idea ._.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Not sure why i'm getting NullReferenceException 0 Answers

How to integrate the FishBun project into unity? 0 Answers

How to set/change variables from an Editor Window on a Prefab 0 Answers

What is the best option for serialization? 0 Answers

Continuously monitor childrens properties 1 Answer


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