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 pauloapsantos96 · Jul 20, 2014 at 02:20 AM · if-statementsrecursionstackoverflow

Stack Overflow in if condition.

I have the following code where i try to recursively call a function:

 if( value == 1 ) function();

I tested and the value is not always 1 but i get a StackOverflow error when running. I can't seem to find the problem. I could use some help solving this.

Thanks in advance.

Edit:

 private void AddNeighbors( Vector2 o, Vector2 s )
     {
         //Debug.Log( "Called" + o );
         int notAdded = 0;
         
         // LEFT //
         int[ , ] left = GetRoom();
         int leftZeroCount = 0;
         for( int x = 0; x < left.GetLength( 0 ); ++x )
         {
             float a = o.x - left.GetLength( 0 ) + x;
             for( int y = 0; y < left.GetLength( 1 ); ++y )
             {
                 float b = o.y + y;
                 if( a > 0 && b > 0 && grid[ ( int )a, ( int )b ] == 0 )
                     ++leftZeroCount;
             }
         }
         if( leftZeroCount == left.GetLength( 0 ) * left.GetLength( 1 ) )
         {
             int add = Random.Range( 0, 2 );
             if( add == 1 )
             {
                 Vector2 origin = new Vector2( o.x - left.GetLength( 0 ), o.y );
                 for( int x = 0; x < left.GetLength( 0 ); ++x )
                 {
                     float a = origin.x + x;
                     for( int y = 0; y < left.GetLength( 1 ); ++y )
                     {
                         float b = origin.y + y;
                         grid[ ( int )a, ( int )b ] = left[ x, y ];
                     }
                 }
                 AddNeighbors(
                     origin,
                     new Vector2( left.GetLength( 0 ), left.GetLength( 1 ) )
                 );
             }
         else ++notAdded;
         }
         else ++notAdded;
         
         // CHECK IF ADDED //
         if( notAdded == 1 ) AddNeighbors( o, s );
     }

The GetRoom function returns an random int[ , ] composed of ones and zeros.

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 Daniel-Everland · Jul 20, 2014 at 02:41 AM 0
Share

To troubleshoot your issue we'll need more information. Could you link the rest of your scrip?

avatar image Kiwasi · Jul 20, 2014 at 03:00 AM 0
Share

For recursives there must be something inside function() that changes value(). Is there?

Please post the entire script for function ()

1 Reply

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

Answer by tanoshimi · Jul 20, 2014 at 10:53 AM

As far as I read it, the possible paths through your function are as follows:

  • leftZeroCount == left.GetLength( 0 ) * left.GetLength( 1 ) (line 19) is true, and add == 1 (line 22) is true, in which case you call AddNeighbors again (line 34).

  • leftZeroCount == left.GetLength( 0 ) * left.GetLength( 1 ) (line 19) is true, and add == 1 (line 22) is false, in which case you ++notAdded; (line 39).

  • leftZeroCount == left.GetLength( 0 ) * left.GetLength( 1 ) (line 19) is false, in which case you ++notAdded; (line 41).

If either of the last two cases happens, then notAdded == 1, in which case you call AddNeighbors again (line 44)

So, whatever happens, this is an endless recursive loop, which is why you're getting a stack overflow.

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 pauloapsantos96 · Jul 20, 2014 at 11:09 AM 0
Share

Thank's a lot the outside else statement didn't need to be there it work's perfectly now.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Huge Lists cause unity to throw bug reporter and crash? 0 Answers

Recursing through a bone hierarchy: Stack Overflow 1 Answer

Stack overflow with recursive floodfill method. 2 Answers

I'm getting StackOverFlowException 1 Answer

A better way to write a recursive if statement like this mess? 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