Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by UnityExample · Feb 24, 2013 at 09:44 PM · c#waitsecondssleep

C# wait X-Seconds in Code before continue in Code?

actually i'm coding a Traffic-light-System. and now i stuck at the point of changing from red to green. How can i wait in the code before it continues, but dont stop the whole Game.

actually i have this, but this stops the whole game

 trafficLight.ChangeStatus(Status.Yellow);
 System.Threading.Thread.Sleep(2000);
 trafficLight.ChangeStatus(Status.Green);

i need to light up the yellow light for about 2 seconds, then it should turn on the Green light, how to do this?

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

3 Replies

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

Answer by robertbu · Feb 24, 2013 at 10:00 PM

Quick example in C#:

 public class TrafficLight : MonoBehaviour {
     
         void Start () {
             StartCoroutine (GreenYellowRed ());
         }
     
         IEnumerator GreenYellowRed()
         {
             while (true) {
                 Debug.Log ("Green");
                 yield return new WaitForSeconds(3.0f);
                 Debug.Log ("Yellow");
                 yield return new WaitForSeconds(1.0f);
                 Debug.Log ("Red");
                 yield return new WaitForSeconds(3.0f);
             }
         }
     }
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 UnityExample · Feb 25, 2013 at 09:26 PM 1
Share

worked perfectly. just added a bool flag outside the while and set it to false after all changes are done so that the while stops, otherwhise the changes will be repeated

avatar image gearpichon · May 01, 2014 at 05:37 AM 0
Share

sorry i'm kind new on this, how can i wait secons in the awake function. Corutines can not be loaded from Awake. I need to wait in the Awake :C

avatar image robertbu · May 01, 2014 at 06:05 AM 0
Share

You can't make Awake() a coroutine, but you can start a coroutine from Awake(). For example, if you replace Start() in the above with with Awake() it would work.

avatar image gearpichon · May 01, 2014 at 06:55 AM 0
Share

you are right i can call it, but this dont stops the code excution the courutine make a paralel process D:, how can i wait X seconds (i need to stop the code excecution)

avatar image robertbu · May 01, 2014 at 07:53 AM 0
Share

This needs to be opened as a new question. You need to explain exactly what you are trying to do.

avatar image
1

Answer by _Grocfex0000 · Oct 20, 2014 at 11:01 AM

im pretty useless scripter but i needed the same thing and so i did it with calculateing stuff and it does not need some kind of really complicated stuff i think this may help you:

 public int one;
 public int two = 50;

if (one <= two){

one = one -1;

if (one == 40){

dosomethingfirst

} if (one == 30){

dosomethingsecond

}

}

if (one == 0){

one = two; }

}

Comment
Add comment · 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
0

Answer by kpetkov · Aug 01, 2018 at 10:50 AM

So far so good. What if I need to wait for some period of time from a class that have to be instantiated and not inheriting from MonoBehaviour ? How can I do that ? Thread sleep is not acceptable, also I don't have acces to coroutines. And by the way I also want to be able to wait in the Utility class where every method is static and have to be accessed from anywhere.

Comment
Add comment · 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

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

13 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 avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

WaitForSeconds Problem [C#] 4 Answers

Multiple Cars not working 1 Answer

Editing the Engine 0 Answers

Changing Mesh of an object depending on Health Value. C# 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