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
1
Question by Clopay · Feb 12, 2014 at 02:49 AM · instantiatecoroutineinterval

Instantiate at Intervals

Hey guys,

I need to instantiate a prefab at a given time interval. I have tried at least 20 different ways, and not one single solution I've found anywhere on the internet has worked. Here's what happens: a prefab gets successfully instantiated after waiting 2 seconds, but then it gets instantiated again every single frame, giving me tons and tons of them. Not what I need. This result has been the same for every method - using a coroutine, using invoke repeating...you name it, I've tried it.

I'm using C#. Any help is greatly appreciated.

Thank you!

Comment
Add comment · Show 2
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 getyour411 · Feb 12, 2014 at 02:49 AM 0
Share

Show the code you have for InvokeRepeating, that seems like a good candidate

avatar image Clopay · Feb 12, 2014 at 02:57 AM 0
Share

I've removed it by now, but what I had was something like this:

 void Update () {
         InvokeRepeating("CreatePrefab", 2.0f, 2.0f);
 }
 
 void CreatePrefab() {
     Instantiate(prefab, spawn.position, Quaternion.identity);
 }

This successfully waits 2 seconds, but then instantiates on prefab per frame it seems.

I'm also trying something like this:

 void Update () {
     StartCoroutine("CreatePrefab");
 }
 
 IEnumerator CreatePrefab() {
         Instantiate(prefab, spawn.position, Quaternion.identity);
     yield return new WaitForSeconds(5.0f);            // 
 }

but it returns the same result.

2 Replies

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

Answer by Wobbegong · Feb 12, 2014 at 03:36 AM

 private float InstantiationTimer = 2f;
 
 void Update () {
     CreatePrefab();
 }
 
 void CreatePrefab()
 {
     InstantiationTimer -= Time.deltaTime;
     if (InstantiationTimer <= 0)
     {
         Instantiate(prefab, spawn.position, Quaternion.identity);
         InstantiationTimer = 2f;
     }
 }

You could change the 2f to a variable for easier control of the wait time if desired.

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 Clopay · Feb 12, 2014 at 03:50 AM 0
Share

That works well! If I change the 2f to a variable, this essentially has the same functionality as InvokeRepeating() in that the InstantiationTimer float is the start delay and the variable would be the delay in between instantiations. Works great.

avatar image
2

Answer by Ashish Dwivedi · Feb 12, 2014 at 03:37 AM

You are calling "InvokeRepeating" in update that's why it is happening. You need to call "InvokeRepeating" only once and that will work according to you what you are saying. Read Invoke repeating reference carefully. Call it into Start.

Comment
Add comment · Show 2 · 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 Clopay · Feb 12, 2014 at 03:49 AM 0
Share

Thank you for your answer, Ashish. Your solution did work as you described; however, I am looking to call the function sometime other than start.

avatar image Ashish Dwivedi · Feb 12, 2014 at 03:56 AM 0
Share

Not an issue. Simply make a public method suppose "StartInstantiation" and call your "Invokerepeating" from it. Now you need to call only this "StartInstantiation" method where ever you want whether from same same script or from another script, means on any event like mouse , button or collision, trigger etc.... Please remember to accept the answer if you think it solved your problem.

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

20 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

Related Questions

Object reference to set to instance on an instantiated object 1 Answer

Delay Collecting Object by Player 0 Answers

Respawn after delay 3 Answers

Instantiating many objects during update lags 1 Answer

How to make a loading feedback for big GameObjects loading? 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