• 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 Lawzer · Feb 10, 2012 at 12:35 AM · instantiatespawntimercounter

Timer to call Instantiate

I've been coding a game where a player must avoid random enemies as they reach him and have written the code to wait a set amount of time before spawning the next enemy. I have a counter in the Update function that should call the random generator function and reset itself when it reaches a certain threshold, however when I run the program it decides to ignore the if statement and execute everything in the Update function every frame. How can I fix this?

 var waitTime: float = 100;
 var counter : float = 0;
 
 function Update () {
 
     //counts up every frame
     counter++;
     print(counter); //temp
     
     //calls enemySpawn when the limit is reached  
     //resets the counter
     if (counter >= waitTime){
         enemySpawn();
         counter = 0;
         }
 
 }
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

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

Answer by Berenger · Feb 10, 2012 at 12:40 AM

For that kind of things, coroutines are just per-fect. Basically, they allow you to stop the execution of a function for a given amount of time, letting the rest of the program run, and then finishing when the time as past. Here is what you can do :

 function Start(){
     Spawn();
 }
 
 function Spawn(){
     while( true ){
         enemySpawn();
         yield WaitForSeconds(waitTime); // That line tell the function to stop for waitTime seconds
     }
 }
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 Lawzer · Feb 10, 2012 at 12:51 AM 0
Share

Thank you that works great :D I'm still having a problem though. The scene works fine at the start but as time goes on, the time between spawns decreases to the point where the enemies are overlapping each other. It is as if something keeps calling the script over and over again or something. Any ideas?

avatar image Berenger · Feb 10, 2012 at 01:21 AM 0
Share

$$anonymous$$ake sure you don't call the function Spawn in Update. It must be call only once. In C#, as what's going is less hidden, I think it's more obvious. Ins$$anonymous$$d of Spawn(), it would be StartCoroutine( Spawn() ), making it clear that you create a new instance of the coroutine. So yeah, call it only once ^^

avatar image Lawzer · Feb 10, 2012 at 01:57 AM 0
Share

I think I was calling it twice in the scene, though I can't be sure. I realised that I could merge my working player script with this level one with ease as I will always have the player onscreen. Now it all works great. Thanks for your help :)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How do i prevent an object instantiating another object straight away 2 Answers

Wave spawning script help 2 Answers

How to fix my enemy Instantiate script? its clone too many 3 Answers

Having Trouble with Instantiating an object on an axis 2 Answers

How would I create a script that spawns objects more frequently as time goes on? 3 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