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 5pointseven · Sep 09, 2014 at 01:25 PM · c#coroutinetimerwaitforsecondsdelay

C# simple delay execution without coroutine?

Hello, everyone. All I want to do is wait for 0.1 second before setting a boolean to true.

Is there a simple way to do it without using coroutine or invoke?

Thanks in advance.

Comment
Add comment · Show 2
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 Jeric-Miana · Sep 09, 2014 at 01:33 PM 0
Share
 public float timer;
 public bool count =true;
 void Update()
 {
 if(count){
 if(timer >=0.1)
 
 {
 yourbool=true;
 }
 }
 }
avatar image 5pointseven · Sep 09, 2014 at 01:54 PM 0
Share

Another important thing Boolean is set outside the update function. It's set when 2 fingers are lifted off the screen to prevent another script to registering a nanosecond while one of fingers is still there as a single touch.

2 Replies

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

Answer by jokim · Sep 09, 2014 at 01:39 PM

I believe coroutines are the better way to do these kind of things as it allows code to keep on running, without freezing the game. Also, they can be used outside of the Update block

But there is workarounds, one of them imply using a timer in the update function with a bit of boolean values like so :

 float timer = 0;
 float timeToWait = 0.1f;
 bool checkingTime;
 bool timerDone;
 
 void Update()
 {
     if (checkingTime)
     {
         timer += Time.deltaTime;
         if (timer >= timeToWait)
         {
             timerDone = true;
             checkingTime = false;
             timer = 0;
         }
     }
 
     if (timerDone)
     {
         //DoSomething()
         timerDone = false;
     }
 }



The only thing left is to set CheckingTime to true somewhere

Comment
Add comment · Show 5 · 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 tanoshimi · Sep 09, 2014 at 01:42 PM 0
Share

Why don't you want to use Coroutines or Invoke with a delay?

avatar image jokim · Sep 09, 2014 at 01:43 PM 0
Share

well, Personally, i'd use coroutines, but the OP probably has his reasons.

I actually didn't think of the Invoke, that's another good way to go about it. $$anonymous$$uch shorter that doing that whole update Timer

avatar image TimLoo6 jokim · Mar 03, 2021 at 11:41 AM 0
Share

Invoke is shorter if you're not going to use parameters for whatever function you want to execute That's why I stopped using it

avatar image tanoshimi · Sep 09, 2014 at 01:45 PM 0
Share

Sorry @jokim - had intended this to be a comment under the question to the OP, not a comment on your answer!

avatar image 5pointseven · Sep 09, 2014 at 01:52 PM 0
Share

@tanoshimi I use Invoke currently, but my inner perfectionist doesn't want to create a dedicated function for one line of code. And I don't need all power of a coroutine for one simple thing.

avatar image
-2

Answer by rich2020 · Jan 24, 2017 at 11:23 PM

I know this is an old post, but I have a nice solution and thought I'd share it. An added bonus is that no Windows interrupt rate artifacts will be encountered:

 public static void Wait(long milliseconds)
 {
     var sw = Stopwatch.StartNew();
     var ticks = (milliseconds * Stopwatch.Frequency) / 1000;
     while (sw.ElapsedTicks < ticks) { }
 }
Comment
Add comment · Show 3 · 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 jdelange · Oct 22, 2017 at 02:13 PM 0
Share

Solved my issue: delaying program execution without the need to split up the code into coroutine(s). Used System.Diagnostics.Stopwatch, as otherwise I get a conflict with Debug which is also defined in Unity.

avatar image Bunny83 · Oct 22, 2017 at 02:39 PM 0
Share

This makes no sense. Blocking the whole application is something you never want to do. During that time no visual update happens and if it takes too long the OS might flag your application as "not responding" and might even kill your application.

Apart from that just blocking for a certain amount of time is done by using

 System.Threading.Thread.Sleep(milliseconds);

Which does blocking the current thread for the specified amount of time. Using Sleep doesn't eat up any CPU performance unlike a spinning while loop.

avatar image jdelange Bunny83 · Oct 22, 2017 at 03:54 PM 0
Share

@Bunny83: In my case it does work (turn based game), but I agree that Sleep is the better way to do this especially on mobile saving power. So I have switched to the Sleep implementation. Thanks.

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

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can i wait seconds without a coroutine (C#)? 4 Answers

Better way for a timer to load a level? 1 Answer

I want to have cancelinvoke start multiple seconds arfter object becomes invisible 1 Answer

How to get precise spawn interval (C#)? 3 Answers

WaitForSeconds Not Working 4 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