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 /
This question was closed Dec 07, 2017 at 10:12 PM by imM4TT for the following reason:

The question is answered, right answer was accepted Thx <3

avatar image
0
Question by imM4TT · Dec 07, 2017 at 05:21 PM · functions

Question about delay

Hi, im trying to increase the mouvement speed object in a defined period like 10s. I wonder how to integrate a notion of delay. I was thinking about a counter and a function while, but i dont know how to specify that the counter must be incremented every second and how Or maybe is there an another function for that ?

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

  • Sort: 
avatar image
1
Best Answer

Answer by MacDx · Dec 07, 2017 at 05:49 PM

Coroutines are the answer, they're excellent for stuff like this. The allow you to delay the execution of code, and they run in "parallel" to the update method. I'll give you a small example

 void Start()
 {
     StartCoroutine(Counter(10));
 }

 private IEnumerator Counter(int countLimit)
 {
     int count = 0;
     while(count < countLimit)
     {
         yield return new WaitForSeconds(1f); // 1 second delay
         //Do stuff after the delay
         print("Increasing movement speed");
         print("Count: " + count);
     }
 }

So this creates a counter that gets increased every second. In this example I start the coroutine at Start however you can start a coroutine wherever and whenever you want (Careful with doing it inside an Update method because you could potentially create a ton of coroutines, one each frame at 60 frames per second, that is a lot and they will all run in "parallel" which can cause and will probably cause unexpected behaviour so be warned, if you mean to only use one at a time, then make sure only one gets started). There's a lot of stuff you can do with coroutines, so I suggest you to investigate them a little bit further. https://docs.unity3d.com/Manual/Coroutines.html

Hope this helps!

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 imM4TT · Dec 07, 2017 at 06:09 PM 0
Share

Thanks for help ! I tried to use your example script but nothing happens on my screen. I can see a function print, that's why i wonder. Im trying to learn about this Coroutine function on your link

avatar image MacDx imM4TT · Dec 07, 2017 at 06:17 PM 0
Share

Nothing happens on screen because there is no code there that will make something happen on screen, you'll only get console prints. But that's the point, it is just an example, you should be able to fill/use that with the actual code for your game.

avatar image imM4TT MacDx · Dec 07, 2017 at 07:06 PM 0
Share

Yep I understand now, thanks ! if you can help me for this 2nd problem that would be really nice, otherwise ill do without :x. I have something like that in my script, but something is wrong with my boolean, that said me unexpected symbol 'alright'.A big thank you if you help me ... private bool alright = true; void Update(){ if ( (Y) && (alright == true) ){ StartCoroutine( delay () ) } IEnumerator delay() { yield return new WaitForSeconds(10f) alright = true; }

Show more comments

Follow this Question

Answers Answers and Comments

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

Unity 4.6 Button OnClick's Functions not showing. 9 Answers

[solved] This error "NullReferenceException: Object reference not set to an instance of an object" occurs only the first time? 0 Answers

The DontDestroyOnLoad function appears without being called. What's going on? 0 Answers

Adding textures to different faces of 3d furniture parts?,Adding texture to only one face of a table 1 Answer

Difference between C# Invoke and Unity Invoke 0 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