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 tdgs · Dec 02, 2012 at 10:29 PM · invokerepeatingstartcoroutine

Invoke repeating and StartCoroutine

I'm building a 2d shooter and I want to have a boss which every 5 seconds shoots 3 bullets at the player with 0.5 seconds delay between them , so I would have something like this:

Boss shoots --0.5 seconds-- Boss shoots --0.5 seconds-- Boss shoots --5 SECONDS-- Boss shoots --0.5 seconds-- Boss shoots --0.5 seconds-- Boss shoots --5 SECONDS

Now what I tried was this:

 void Start () {InvokeRepeating("trigger",0f,5f);}
 
 IEnumerator trigger(){
         yield return StartCoroutine("ripi_fire");
 }
     
 
 IEnumerator multi_fire(){
         counter = 3;
         while(counter > 0 ){
             fire ();
             yield return new WaitForSeconds(0.5f);
             counter-- ;
         }
         
 }
 
 
     
 void fire(){
     Instantiate(enemy_bullet,this.transform.position, Quaternion.LookRotation(target.transform.position- this.transform.position));
 }

But when I try that unity crashes. I read somewhere else that you cannot use Startcoroutine with Invoke. Is it the same with InvokeRepeating? If so, how are you supposes to generate an event like that?

Yield return new WaitForSeconds is the best way to add delay between 2 events and InvokeRepeating is the best way to repeat an event after every X seconds.

Any suggestions?

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
4

Answer by coastwise · Dec 03, 2012 at 02:52 AM

Why not roll the whole functionality into a single coroutine like so:

 void Start () {
     StartCoroutine("FireThriceAndWait");
 }
 
 IEnumerator FireThriceAndWait () {
     while (true) {
         fire();
         yield return new WaitForSeconds(0.5f);
         fire();
         yield return new WaitForSeconds(0.5f);
         fire();
         yield return new WaitForSeconds(5f);
     }
 }
 
 void fire(){
     Instantiate(enemy_bullet,this.transform.position, Quaternion.LookRotation(target.transform.position- this.transform.position));
 }
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 unity_WPzfLehdpNGaDQ · Sep 12, 2019 at 11:07 AM 0
Share

Is it a good idea to have a while loop in a Coroutine. I mean in terms of performance?

avatar image xxmariofer unity_WPzfLehdpNGaDQ · Sep 12, 2019 at 12:04 PM 0
Share

a while loop will not affect a performance, it depends on what it is inside the while loop, but it will basically behave the same as an update event, if you have any specific situation or code i can try and help you

avatar image JohnLadder xxmariofer · Mar 03, 2021 at 10:02 AM 0
Share

For better performance create 2 variables:


 private WaitForSeconds delay05 = new WaitForSeconds(0.5f);
 private WaitForSeconds delay5 = new WaitForSeconds(5f);


Then, use them like this:


 IEnumerator FireThriceAndWait () {
      while (true) {
          fire();
          yield return delay05;
          fire();
          yield return delay05;
          fire();
          yield return delay5;
      }
  }


This way you only create WaitForSecond Variables 3 times. The way you are doing it always creates new variables.

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

13 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

Related Questions

Shooting Projectiles With controlling attackSpeed 0 Answers

What is the best between StartCoroutine or InvokeRepeating. 5 Answers

Custom fixed update cycle? 6 Answers

InvokeRepeating not executing - recently updated unity 1 Answer

If you disable a gameobject, does an InvokeRepeating loop end or pause? 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