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 RandomUser123 · Aug 22, 2014 at 05:34 PM · coroutineupdatestartinvokerepeating

Issues with InvokeRepeating in Start/Update functions

I'm still trying to learn Invokes and Coroutines and I think I have them almost down. I have this snippet of code here:

         if(distance <= 40)
         {
             InvokeRepeating ("Spawn", 5, 5);
         }
         
         else
         {
             CancelInvoke ("Spawn");
         }

I want to spawn an item once every 5 seconds if I'm within a distance and do nothing if I'm not within distance. I have tried this code in my Start and Update functions.

If in Start, the item is spawned but never stops spawning after I leave the target distance.

If in Update, it works but it Spawns the item every frame creating hundreds of them.

I've also tried using coroutines using this code:

     IEnumerator Spawn()
     {
         yield return new WaitForSeconds(1);
         while(true)
         {
             Instantiate(missile,transform.position,Quaternion.identity);
             yield return new WaitForSeconds(randomTime);
         }
     }

and the calling Start/Stop Coroutine in the if/else statement above but it has the same results.

Can anyone explain how I would achieve what I'm looking for? 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
4
Best Answer

Answer by Landern · Aug 22, 2014 at 05:39 PM

Check to see if it's already invoking, if it is, don't invokerepeating again.

 if(distance <= 40 && !IsInvoking("Spawn"))
 {
     InvokeRepeating ("Spawn", 5, 5);
 }
 else
 {
     CancelInvoke ("Spawn");
 }
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 RandomUser123 · Aug 22, 2014 at 06:17 PM 0
Share

Thanks for the response @Landern, unfortunately this did not work I'm afraid, the items don't spawn at all now in the Update function and when in the Start function, they still spawn even when out of range

avatar image RandomUser123 · Aug 22, 2014 at 06:39 PM 0
Share

Never $$anonymous$$d, I changed the else to an else if and everything works now :) Thanks for the help

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

23 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

Related Questions

Update scene every 5 minutes 2 Answers

How do I replace Invoke Repeating to allow a repeat rate change 1 Answer

convert timer from update() to coroutine or invokerepeating 0 Answers

inconsistent results calculating the distance moved by an object 1 Answer

Script and Start function of script are running, but Update function is not? 0 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