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 Felipetnh · Jan 23, 2012 at 02:28 PM · updatetimeafter

Start to Update() after X seconds

Hi everybody... I have a question:
I'm creating a Tower Defense game in Unity and I'm doing the wave control now.
I want to start the wave object's update after X seconds, like most of the Tower Defense games we see.
Example:
My game starts and all the monsters coming from the first wave are released.
After the first wave is done I disable it and enable the second wave. And then I want that the second wave start to update (because it is in the Update method that I control all the monsters) after a pre-determined time.


How can I do this?

I'm open for any suggestions.

Thanks in advance.

Comment
Add comment · Show 1
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 Fattie · Jan 23, 2012 at 09:56 PM 0
Share

You simply need to learn about the "Invoke" command. Nothing to it ....

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by syclamoth · Jan 23, 2012 at 03:03 PM

Try shifting all the logic you would usually put in Update into a coroutine that gets set off by Start. That way, you can put a

 yield WaitForSeconds(x seconds);

before beginning on the rest of the logic! There is no way to actaully delay the Update function without freezing the entire game- but there are plenty of ways to achieve what you are trying to do without doing that.

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 Statement · Jan 23, 2012 at 04:24 PM

I prefer coroutines for tasks like these.

  • Start() Your starting point that will run 3 waves of monsters. After for loop, show a victory screen for example.

  • BeginWave(var wave : int) Put setup code for the wave there, like spawning enemies or changing the music.

  • RunWave(var wave : int) Put update code the wave inside the while loop. Remember to yield; or it crash.

  • EndWave(var wave : int) Put cleanup code for the wave there, like removing enemies or changing the music.

The WaitForSeconds are optional and was only meant to illustrate how you can delay the code at various points.


 function Start() {
     for (var wave = 0; wave < 3; ++wave) {
         yield BeginWave(wave);
         yield RunWave(wave);
         yield EndWave(wave);
     }
 }
 function BeginWave(var wave : int) {
     // Set up the wave, if needed...
     yield WaitForSeconds(5);        
 }
 function RunWave(var wave : int) {
     while (true) {
         // Update your game logic as before.
         // 'break;' (or 'yield break;' if in nested loop) to end wave.
         yield;
     }
 }
 function EndWave(var wave : int) {
     // Clean up the wave, if needed...
     yield WaitForSeconds(5);
 }


Or if you are more of a compact code fan (I'm not):

 function Start() {
     for (var wave = 0; wave < 3; ++wave) {
         // Set up the wave, if needed...
         yield WaitForSeconds(5);
         while (true) {
             // Update your game logic as before.
             // 'break;' to end wave.
             yield;
         }
         // Clean up the wave, if needed...
         yield WaitForSeconds(5);
     }
 }

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 Owen-Reynolds · Jan 23, 2012 at 05:57 PM 1
Share

I use the same coroutine trick, but create a prefab with a "spawn script" on it. When the player presses begin, instantiate the spawn script for that level. It waits 3 secs, spawns a wave, waits more, then spawns the other two waves on the level.

It hurts my head to have the level logic (level preview, help menu, You lose -- retry?) in the same script as the level.

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

8 People are following this question.

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

Related Questions

Time.deltaTime not consistent over time 1 Answer

fixed update and time.deltatime 1 Answer

How does Time.deltaTime provide smoother physics functions? (Frame rate question) 2 Answers

Updating an array indices dynamically 2 Answers

Add 1 Per Second to an Int 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