Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by Capricornum · Jan 25, 2018 at 02:46 PM · collider2dcollision2drecursionstackoverflow

[Solved] Recursive Method causing Stack Overflow

Dear Community,

You all know games like "Bejeweled" or "Candy Crush". I am trying to copy it, as a learning experience. But a little different. I have a grid like this:

alt text

I don't move tiles. Instead if I click on one tile I want the game to detect what color it is. Then I want all the horizontal and vertical neighbors of the same color to be detected and their neighbors of the same color too. Like a cascade. So lets say I click on the sun at the bottom right of the picture, I want all 3 suns at the bottom right to be detected, grouped and deactivated.

I have come as far as grouping the immediate neighbors of the clicked tile. But if I repeat the same procedure for the neighbors neighbor using a recursive method I get a "Stack Overflow" Error. This is my code. This script is attached to each tile on the game board:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Element : MonoBehaviour {
     
     public LayerMask elementLayer;
     public bool hasBeenChecked;
     
     private LayerMask familyLayer;
     private List<Element> family = new List<Element> ();
     
     void OnMouseDown ()
     {
         Debug.Log ("Mouse has been clicked on: " + this.transform.position);
         familyLayer = this.elementLayer;
         CheckNeighbours (this.transform.position);
     }
 
     void CheckNeighbours(Vector2 newPos)
     {
         Collider2D[] collidersInRange = 
             Physics2D.OverlapBoxAll (newPos, new Vector2 (1f, 1f),  45f, familyLayer);
 
         foreach (Collider2D col in collidersInRange) 
         {
             if (!hasBeenChecked) {
                 Element elemToCheck = col.GetComponent<Element> ();
                 family.Add (elemToCheck);
                 elemToCheck.hasBeenChecked = true;
                 CheckNeighbours (elemToCheck.transform.position);
             }
         }
     }
 }

My approach using a recursive method doesn't seem to work. I have run out of ideas, as to how to program this game. I would greatly appreciate your support. Thanks.

playfield-2.jpg (151.1 kB)
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
1
Best Answer

Answer by Bunny83 · Jan 25, 2018 at 04:15 PM

Ok first address your direct issue. "Physics2D.OverlapBoxAll" returns a list of all colliders around the given position. Before you call your method recursively you set "hasBeenChecked" of the Element of each found collider to true. However inside your "CheckNeighbours" method you only check "hasBeenChecked" of the start element.

You would have to do something like:

 hasBeenChecked = true;
 family.Add(this);
 foreach (Collider2D col in collidersInRange) 
 {
     Element elemToCheck = col.GetComponent<Element> ();
     if (!elemToCheck.hasBeenChecked) {
         family.Add (elemToCheck);
         elemToCheck.hasBeenChecked = true;
         CheckNeighbours (elemToCheck.transform.position);
     }
 }


However for grid games you don't want to use "OverlapBoxAll". You should define an array or two dimensional array to manage all your elements. That way you can directly access the 4 neighbors by adding subracting the index in the array.


Though if you want to keep your current approach the fix i mentioned about should solve your stack overflow. Keeo in mind to clear the "family" list before you let the user click again. Also you need to reset all those "hasBeenChecked" variables in order for them to participate again in such a search. Of course if you remove / destroy those objects it's not necessary.

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 Capricornum · Jan 25, 2018 at 05:46 PM 0
Share

Thank you ever so much. It is working now. Also thanks for the re$$anonymous$$der about clearing the list and the "hasBeenChecked" variables. Just to make sure I am learning the lesson that is to be had here: When I typed

 if (!hasBeenChecked) {
 ...
 }

i essentially check for "this.hasBeenChecked", which will always be the start element. Is that what went wrong? It makes sense. Thank you. Danke :-)

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

125 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D Game - Get collider name 1 Answer

How can i optimize colliders in my character? ( character stuck) 0 Answers

OnCollisionEnter2 problem. 1 Answer

Stack Overflow Exception on recursive functions 1 Answer

2d balls stuck in box colliders 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