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 Supershandy · Jun 30, 2013 at 04:06 PM · instantiatewaitforsecondsinvokerepeating

InvokeRepeating affecting parts of script

Hey guys, I need some help,

I have a random enemy wave generator set up and although "InvokeRepeating" works for the swarm ships and circle ships, for some bizarre reason it doesn't work for the wave ships and the waveTimer stops after one second when the Circle function is launched.

Here is the code

 #pragma strict
 
 public var spawners : Transform[];
 public var asteroid : Transform;
 public var swarmShip : Transform;
 public var waveShip : Transform;
 public var waveShipR : Transform;
 public var selection : int;
 public var waveTimer : float;
 public var countdown = false;
 public var Timer : int;
 
 function Start () 
 
 {
     InvokeRepeating ("Generator", 40.0f, 40.0f);
     yield WaitForSeconds (10);
     audio.Play();
     InvokeRepeating ("Asteroid", 0.01f, 0.5f);
     yield WaitForSeconds (20);
     CancelInvoke("Asteroid");
     
 }
 
 function Update()
 
 {
     Timer = Time.time;
     if ((selection == 0) || (selection == 1) || (selection == 2) && (waveTimer <= 30) && (countdown == false))
     {
         waveTimer -= Time.deltaTime;
         countdown = true;
         
         if (waveTimer <= 0.0)
         {
             countdown = false;
             waveTimer = 30.0;
             selection = -1;
         }
     }
     Debug.Log ("Wave Time - " + waveTimer);
 }
 
 function Generator()
 
 {
     selection = -1;
     
     if (selection == -1)
     {
         selection = Random.Range(0, spawners.length);
     }
     
     if (selection == 0)
     {
         audio.Play();
         InvokeRepeating ("Swarm", 0.5f, 0.5f);
         if (waveTimer <= 0.0)
         {
             CancelInvoke ("Swarm");
             selection = -1;
         }
     }
     
     if (selection == 1)
     {
         audio.Play();
         Wave();
         WaveR();
         
         if (waveTimer <= 0.0)
         {
             selection = -1;
         }
     }
     

 if (selection == 2)
         {
             audio.Play();
             InvokeRepeating ("Circle", 5.0f, 5.0f);
             if (waveTimer <= 0.0)
             {
                 CancelInvoke ("Circle");
                 selection = -1;
             }
         }
 }
 
 function Asteroid ()
 
 {
     var vec = Vector3 (transform.position.x, Random.Range (-3.5f, 5.4f));
     Instantiate(asteroid, vec, transform.rotation);
 }
 
 function Swarm () 
 
 {
     var vec = Vector3 (transform.position.x, Random.Range (-3.5f, 5.4f));
     Instantiate(swarmShip, vec, transform.rotation);
 }
 
 function Wave ()
 
 {
     for (var i : int = 0; i < 5; i++)
     {
         Instantiate(waveShip, spawners[1].position, spawners[1].rotation);
         yield WaitForSeconds (0.4);
     }
 }
 
 function WaveR ()
 
 {
     for (var j : int = 0; j < 5; j++)
     {
         Instantiate(waveShipR, spawners[1].position, spawners[1].rotation);
         yield WaitForSeconds (0.4);
     }
 }
 
 function Circle () 
 
 {
     var vec = Vector3 (transform.position.x, Random.Range (-3.5f, 5.4f));
     Instantiate(spawners[2], vec, transform.rotation);
 }

These are the sections causing problems.

 if (selection == 1)
         {
             audio.Play();
             Wave();
             WaveR();
             
             if (waveTimer <= 0.0)
             {
                 selection = -1;
             }
         }
 if (selection == 2)
         {
             audio.Play();
             InvokeRepeating ("Circle", 5.0f, 5.0f);
             if (waveTimer <= 0.0)
             {
                 CancelInvoke ("Circle");
                 selection = -1;
             }
         }

In the inspector, the selection number shows "1" and the waveTimer starts counting down, however, nothing appears when I try "InvokeRepeating ("Wave", 3.0f, 3.0f)" and "InvokeRepeating ("WaveR", 3.0f, 3.0f)". When they are set as above with just Wave() and WaveR() it works, but only instantiates one set of them and that's it.

With Circle function, the waveTimer starts and then just stops after one second, the other waves spawn fine, it just means that the circle wave keeps spawning.

And if I try to use "yield WaitForSeconds", it seems to break the Generator function.

Any ideas?

Any other ideas of how to repeat a function?

Thanks

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
1

Answer by Eric5h5 · Jun 30, 2013 at 05:17 PM

You can't use coroutines with InvokeRepeating.

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 Supershandy · Jun 30, 2013 at 06:37 PM 0
Share

Ok, I figured that much from the WaitForSeconds breaking the generator function, So why does the Swarm() function work even though it has InvokeRepeating on it's Generator function event and the InvokeRepeating doesn't for the Wave effect, and the waveTimer stops working on the circle function even though the InvokeRepeating works?

avatar image Supershandy · Jun 30, 2013 at 06:38 PM 0
Share

Also, that answer hasn't really answered the question.....

avatar image Supershandy · Jun 30, 2013 at 07:35 PM 0
Share

Ok, I managed to get the waveTimer working by splitting up the if statement in the Update function into separate parts rather then having the OR statement to test to see which selection has been made, however the original problem still remains with the Wave and waveR function not repeating.

I thought with the changes it might work, but see$$anonymous$$gly it doesn't want to repeat.

This is the code I am using

 if (selection == 1)
     {
         audio.Play();
         InvokeRepeating ("Wave", 0, 4.0f);
         InvokeRepeating ("WaveR", 0, 4.0f);
     }

and the check in the Update function

 if ((selection == 1) && (waveTimer <= 30))
     {
         waveTimer -= Time.deltaTime;
         
         if (waveTimer <= 0.0)
         {
             waveTimer = 30.0;
             selection = -1;
             CancelInvoke ("Wave");
             CancelInvoke ("WaveR");
         }
     }

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

16 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

Related Questions

Wait For Seconds Instantiate Inside For Loop 2 Answers

Pause InvokeRepeating? 1 Answer

Bulletshells not working as intended 2 Answers

Invoke on Struct or some else time wait 1 Answer

InvokeRepeating doesn't work 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