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 Nerethar · Jan 03, 2014 at 02:16 AM · c#crashrecursion

A simple recursion leads to crash

Hi foks, I've got a little problem:

I've got an array that is filled with lists. The list variates in size. And I got a function that needs to walk trough all the entries of this lists and then jump to the next list entry (that could be zero as well.)

I made a short little script but every time I want to execute it unity crashes, and I don't know why.

Here is an exampe of my code:

     private int Speedlevel = 0;
     private int Speedlevelindex = 0;
     public GameObject aktiveFigur;
     
     private List<GameObject>[] Zugreihenfolge = new List<GameObject>[5];
 
 
     private void Reihenfolge(){
         
         if(Zugreihenfolge[Speedlevel].Count>0 && Speedlevelindex<Zugreihenfolge[Speedlevel].Count){
                 aktiveFigur = Zugreihenfolge[Speedlevel][Speedlevelindex];
                 Speedlevelindex++;
                 Kampfrunde();
                 return;
         }else{
             Speedlevel++;
             Speedlevelindex = 0;
             if(Speedlevel>4){
                 Speedlevel = 0;
             }
             Reihenfolge();
         }
     }
 
     private void Kampfrunde(){
         if(aktiveFigur.GetComponent<Figur>().KI=true){
             print ("Hello World" + aktiveFigur.name);
             Reihenfolge();
         }
     }
 


Thanks for any help and good night.

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 HappyMoo · Jan 03, 2014 at 05:56 AM 0
Share

Where do you initialize the Array? You don't check for Zugreihenfolge[Speedlevel]==null, are you sure all are filled? What do you mean with crashes? Can you step through the script and see where it crashes?

avatar image RudyTheDev · Jan 03, 2014 at 11:23 AM 0
Share

The way it's written, it will loop infinitely (having Unity hang up forever). Every branch leads to Reihenfolge() being called again. You need to have an exit condition somewhere.

1 Reply

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

Answer by Imankit · Jan 03, 2014 at 06:39 AM

Your recursion is getting into an infinite loop thats why your Unity is crashing at runTime.. You can do two things -

  1. Make the function IEnumerator and give a wait of 0.034f sec before calling the function in else -

     private IEnumerator Reihenfolge(){
          
         if(Zugreihenfolge[Speedlevel].Count>0 && Speedlevelindex<Zugreihenfolge[Speedlevel].Count){
         aktiveFigur = Zugreihenfolge[Speedlevel][Speedlevelindex];
         Speedlevelindex++;
         Kampfrunde();
         return;
         }else{
         Speedlevel++;
         Speedlevelindex = 0;
         if(Speedlevel>4){
         Speedlevel = 0;
         }
         yield return new WaitForSeconds(0.034f);
         StartCoroutine(Reihenfolge());
         }
         }
    
    
  2. Just make sure that your function does not go into an infinite loop by making if conditions

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 Nerethar · Jan 03, 2014 at 01:16 PM 0
Share

Thank you very much, that line yield return new WaitForSeconds(0.034f); worked out perfectly. I tried it yesterday with a coroutine and added the line just to skip one frame, but this how ever didn't work. Why does it work with this specific time?

Any way, thanks again =)

avatar image HappyMoo · Jan 03, 2014 at 01:23 PM 1
Share

0.034 seconds is close to 30fps ... Butt it's not a good idea to skip with time values if you want to skip to the next frame. This time value will skip two frames on most systems anyway.... To skip just one frame do this:

 yield return null;
avatar image jackson_31 · May 19, 2016 at 10:45 PM 0
Share

Thank you for this. I have done a lot of searches for the solution to these infinite loops and this is the only one that explains it so simply.

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

22 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Why does Unity crash? (and other programs in general) 1 Answer

App crashes after splash screen with Google Play Services for AR update (after April 11th 2020) 2 Answers

This jump code crashes the editor! 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