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 bhartu · Jul 25, 2014 at 06:22 AM · performancecoroutinewhile-loop

start coroutine everytime

hello,

I am working on space shooter like game.

I want to spawn enemies with frequent time delay.

My Question is should i start coroutine everytime, or place a while loop in one coroutine so that i do not have to start coroutine everytime ??

which is better?

does it(while loop option) affect in performance issue or not??

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 sethuraj · Jul 25, 2014 at 06:30 AM 0
Share

To do something freqently with a delay you can always use InvokeRepeating("FunctioName",startdelay,repeattime);

 void Start()
 {
   //Will Instantiate a new enemy every 2 seconds 
   InvokeRepeating("SpawnEnemies",1.0f,2.0f);
 }
 
 //Function to spawn enemies
 void SpawnEnemies()
 {
   GameObject Enemy = Instantiate(EnemyPrefab,transform.position,transform.rotation);
 }

avatar image bhartu · Jul 25, 2014 at 06:37 AM 0
Share

thanks sethuraj for ur answer.

i know about Invoke Reapeating.

but InvokeRepeating will spawn enemy with static time(2 seconds).

my requirement is like, Initially there should be Timedelay of two seonds. but as my ship travels higher, i want to reduce TimeDelay then?

2 Replies

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

Answer by RedDevil · Jul 25, 2014 at 06:39 AM

I just put my function:

 void Start()
 {
 StartCoroutine(FunctionName());
 }

 IENumerator FunctionName()
 {
 while(true)
 {
 yield return new WaitforSeconds(time you want);
 instantiate enemy
 yield return new WaitforSeconds(time you want);
 }
 }

I found this to work better then invoke repeating

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 bhartu · Jul 25, 2014 at 06:54 AM 0
Share

Ok Thanks RedDevil.

avatar image Tehnique · Jul 25, 2014 at 08:12 AM 1
Share

Red, please format that code, use the "101010" button.

avatar image
0

Answer by Tehnique · Jul 25, 2014 at 06:27 AM

Sounds like you need to Invoke something Repeatedly....ta-daa: InvokeRepeating

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 bhartu · Jul 25, 2014 at 06:52 AM 0
Share

yes exactly Technique.

but again my problem is as above as i have commented in sethuraj's answer.

avatar image Tehnique · Jul 25, 2014 at 08:06 AM 0
Share

If you need to invoke it 10 times for 2 seconds, then 10 times for 3 seconds and so on, you can use CancelInvoke to stop all current invokes, and then InvokeRepeating again to define a new time interval.

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

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

Does a coroutine do all its work during one frame? 1 Answer

UI Text not changing color [C#] 2 Answers

Rotate GameObject using Coroutine 2 Answers

When there is a while-loop in a Coroutine, if I have a "yield" within the while-loop, does that mean the Coroutine will be re-entered within the while loop? 1 Answer

Coroutine loop not working 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