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 Caiuse · Dec 16, 2012 at 12:48 PM · crash

Recursive Quadtree causing sudden fail

Got a problem with creating a Quadtree in Unity, on my Start function I create an instance of one Quadtree, the idea is that the quadtree subdivides itself into 4 children per node.

The problem is that as soon as I hit play Unity crashes instantly. Not like a freeze it just fails and disappears on Mac OSX.

I can't understand because I can't see how it could be a infinite loop as the max_level will stop that.

 public class Quadtree {
         
         public Quadtree[] children = null;
         
         public int m_max_level = 4;
         public int m_level = 0;
         
         public Quadtree(int maxLevel, int level){
             m_max_level = maxLevel;
             m_level = level;
             Divide();
         }
         
         public void Divide(){
             if(m_level < m_max_level){
                 children = new Quadtree[4];
                 for(int i = 0; i < children.Length; i++){
                     children[i] = new Quadtree(m_max_level,m_level++);
                 }

} } }

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
2
Best Answer

Answer by Bunny83 · Dec 16, 2012 at 01:17 PM

The answer is quite simple. You use the post increment operator. As you might / should know the post increment operator evaluates the value of the variable before it's actually increasing the value. So when calling:

     children[i] = new Quadtree(m_max_level, m_level++);

when m_level is "0" it will pass the value 0 to the constructor and then increment the value. This will effectively always pass the original value to each recursion without any change.

Use either the pre increment operator or increment the value before you call the constructor.

     // pre increment
     children[i] = new Quadtree(m_max_level, ++m_level);

Since you actually don't need the incremented value inside the class the usual way is to do it like this:

     children[i] = new Quadtree(m_max_level, m_level + 1);

edit
Another very common way for recursive break conditions is to count backwards:

 public class Quadtree
 {
     public Quadtree[] children = null;
 
     public Quadtree(int maxLevel)
     {
         if (maxLevel > 0)
             Divide(maxLevel - 1);
     }
     
     public void Divide(int maxLevel)
     {
         children = new Quadtree[4];
         for(int i = 0; i < children.Length; i++)
         {
             children[i] = new Quadtree(maxLevel);
         }
     }
 }
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 Caiuse · Dec 16, 2012 at 04:56 PM 0
Share

I actually realised this myself, silly silly oversight. I think because the crash behaviour was usual I thought there was something serious going wrong. Thanks though

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

12 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

Related Questions

Raycast Hit App Crashes on touch 0 Answers

Unity freezes on Quit (4.3) 2 Answers

A simple recursion leads to crash 1 Answer

Unity full memory crash, during AssetDatabase.LoadAssetAtPath 0 Answers

Standalone server crash 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