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 greatwhiteshark17283 · Apr 22, 2014 at 07:52 PM · javascriptfunctionloopinvokefor

How to loop a function?

I've heard of the for and while things but I just don't understand them. What I want to do is something like this:

 var cookies = 0;
 MyFunction();
 
 function MyFunction(){
 cookies ++;
 yield WaitForSeconds(5);
 MyFunction();
 }

Whenever I try and do this, I keep getting an error message. I know about using InvokeRepeating, but I already used it in this script with the CancelInvoke, so that would mess everything up if I used it again. So how would I be able to do this?

Comment
Add comment · Show 3
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 koray1396 · Apr 22, 2014 at 08:00 PM 0
Share

you should use

 StartCoroutine($$anonymous$$yFunction());

ins$$anonymous$$d of

 $$anonymous$$yFunction();
avatar image koray1396 · Apr 22, 2014 at 08:02 PM 0
Share

also as I can understand while and for loops are not what you are looking for, they have a different purpose, to process multiple things on a single frame. you should check these; https://unity3d.com/learn/tutorials/modules/beginner/scripting/loops

avatar image greatwhiteshark17283 · Apr 22, 2014 at 08:27 PM 0
Share

The only problem about using a coroutine is that the loop will eventually end. I want the loop to never stop.

2 Replies

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

Answer by chillersanim · Apr 22, 2014 at 08:03 PM

Hello
The code construct you have has an endless recursive call and will cause a stack overflow.

To call the method over and over again, you could use the folowing code (Not tested!):

 var cookies = 0;
 
 function OnEnable()
 {
     // Calls the MyFunction and calls it all 5 seconds
     InvokeRepeating("MyFunction", 0, 5);
 }
 
 function OnDisable()
 {
     // Stops the repeating call
     CancelInvoke("MyFunction");
 }
 
 function MyFunction()
 {
     cookies++;
 }

I hope that the code isn't incorrect because normaly I write in C#.

Greetings
Chillersanim

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 greatwhiteshark17283 · Apr 22, 2014 at 08:35 PM 0
Share

Thanks! I didn't know that it wouldn't interfere if I used invoke twice.

avatar image
0

Answer by Ed unity · Apr 22, 2014 at 08:06 PM

In that script you are never going to escape the MyFunction function. Thus Unity will hang and will appear that it is frozen. What you most likely want to do is something like:

 void Update(){
 int numberOfTimesToRun = 1;
 for(int i = 0; i < numberOfTimesToRun; ++i)
    MyFunction();
 }
 
 function MyFunction(){
 yield WaitForSeconds(5);
 }

Source for the information below: http://www.w3schools.com/js/js_loop_for.asp

"The for loop is often the tool you will use when you want to create a loop.

The for loop has the following syntax:

 for (statement 1; statement 2; statement 3)
 {
   the code block to be executed
 }

Statement 1 is executed before the loop (the code block) starts.

Statement 2 defines the condition for running the loop (the code block).

Statement 3 is executed each time after the loop (the code block) has been executed."

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 chillersanim · Apr 22, 2014 at 08:17 PM 0
Share

Hello

You should not use a yield in a for loop like that when it is called in a repeatedly called method like the Update, because all code after the yield is going to be called late to. Also the update method is going to be called around 60 times per second, so you can cause a stack overflow and you are generating a lot of different handles.

Ins$$anonymous$$d you should use a InvokeRepeating() in the OnEnable() or somewhere where it is not called the whole time.

Greetings
Chillersanim

avatar image greatwhiteshark17283 · Apr 22, 2014 at 08:21 PM 0
Share

I know about invoke repeating and that's what i would have used. The only problem is that I already used it in my script along with Cancel Invoke. If I use it again, then the Cancel Invoke might interfere and then the script won't work properly. I also tried the other method with the for loop and when it was about to do the loop, Unity froze.

avatar image chillersanim · Apr 22, 2014 at 08:24 PM 1
Share

No, you just need to overload the method name and then it will only cancel the invokeRepeating for that method.

As long as you don't call InvokeRepeating() for the same method multiple times (what you realy not should do!), there is no problem.

Edit:
When you use yield in a for loop, then it often happens that unity freezes, as mentioned in the comment above yours.

Greetings
Chillersanim

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

How to correctly shorten this script using arrays and iterations 2 Answers

How do I reset a for loop variable??? 3 Answers

function inside function or while loop (javascript) 1 Answer

Looping a function 1 Answer

Finding nearest object with a certain tag without for loops. 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