• 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
Question by kvazios · Apr 09, 2014 at 01:13 PM · yieldwaitforsecondsdelay

Make delay for spawn

Hello! How I can make delay for spawning mobs in function like -

 function spawnSomet$$anonymous$$ng() {
     var randomSpawn = new Vector3 (Random.Range(-5, 5), 5f, 0f);
     Instantiate(onePrefab, randomSpawn, Quaternion.identity);
 }

If insert yield WaitForSeconds (5), code wait 5 sec and start making objects without stop.

Comment

People who like this

0 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 Simon-Larsen · Apr 09, 2014 at 01:24 PM 0
Share

"start making objects without stop." this can be a pretty heavy task. Sure you don't want to limit the amount of monster? And do you want to spawn them in an interval or just as fast as possible?

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by HarshadK · Apr 09, 2014 at 01:24 PM

I guess you are trying to spawn objects continously over a specific time delay.

If so, you can use InvokeRepeating().

Like t$$anonymous$$s:

 InvokeRepeating("spawnSomet$$anonymous$$ng", 0.0f, 5.0f);

So that your specified function is called after amount of time delay specified.

Comment
Andres-Fernandez

People who like this

1 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 kvazios · Apr 09, 2014 at 01:42 PM 0
Share

Thanks for answer! But nothing is spawned, when i add this.

avatar image kvazios · Apr 09, 2014 at 01:48 PM 0
Share

Okey, with some magic they start to work!

avatar image LightKnight0626 kvazios · Apr 09, 2021 at 08:15 AM 0
Share

what you did to make it work

avatar image

Answer by Patel-Sagar · Apr 09, 2014 at 01:24 PM

after adding yield, from where you are callong t$$anonymous$$s function. you should not call it from Update or OnGUI. It should be called only once if you want to instantiate one. you should call it from start.

Comment

People who like this

0 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 kvazios · Apr 09, 2014 at 01:43 PM 0
Share

if i call it once, they make only 1 object.

avatar image

Answer by wacasce · Apr 09, 2021 at 10:43 AM

InvokeRepeating should work fine. But i suggest you use a for loop to limit the number of instantiated objects, unless if it is one of those endless runner / scrolling games, then you should set your objects to be destroyed after a certain period. try

     int maxEnemies = 100;
     void Start(){
     InvokeRepeating("spawnSomet$$anonymous$$ng",0f,5f); //max # not included or you can use float to include
     }
     
     void spawnSomet$$anonymous$$ng(){
     for (int i = 0; i < maxEnemies; i++){
     //your spawn code here
     }
     }

Comment

People who like this

0 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 wacasce · Apr 09, 2021 at 10:44 AM 0
Share

meant to say max number not included in integer

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

25 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

Related Questions

Using yield wait for seconds in my script, correctly 2 Answers

Loop Animation with delay-variable 1 Answer

How to Delay A Ball shooting script 1 Answer

How to delay a function (how to use WaitForSeconds() and yield) 1 Answer

Coroutine not running after yield return new WaitForSeconds 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