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
0
Question by TheUberMedic · Mar 09, 2018 at 11:54 AM · recursionstackoverflow

Stack Overflow error in recursive method

I'm trying to use a recursive algorithm to solve a maze. How I'm trying to get it to work is to create a list of stacks of vectors. The algorithm will check the latest stack and peek the top which then creates another stack containing that vectors neighboughing cells that can be visited. Once the player's vector location is found, the program stops and gets the top of all the stacks in the list as the path to follow. The problem is that it creates a stack overflow error and crashes Unity once alert is set to true. I know that the program works if I make it iterative but that won't always find the player's location if they're too far away.

 public class EnemyMovement : MonoBehaviour {
 
     public bool Alert;
     public GameObject player;
     private Vector2 PLoci;
     private bool[,,] grid;
     private bool[,] VisitedCells;
 
     private static void PathFinder(List<Stack<Vector2>> movement, bool[,,] grid, Vector2 PLoci, bool[,] VisitedCells)
     {
         if (movement[movement.Count - 1].Peek() != PLoci)
         {
             var temp = movement[movement.Count - 1].Peek();
             int x = (int)temp.x;
             int z = (int)temp.y;
             VisitedCells[x, z] = true;
             movement.Add(new Stack<Vector2>());
             if (grid[x, z, 0] == false)
             {
                 if(z + 1 < Maindata.Size)
                 {
                     if (!VisitedCells[x, z + 1])
                     {
                         movement[movement.Count - 1].Push(new Vector2(temp.x, temp.y + 1));
                     }
                 }
             }
             if (grid[x, z, 1] == false)
             {
                 if (x + 1 < Maindata.Size)
                 {
                     if (!VisitedCells[x + 1, z])
                     {
                         movement[movement.Count - 1].Push(new Vector2(temp.x + 1, temp.y));
                     }
                 }
             }
             if (grid[x, z, 2] == false)
             {
                 if (z - 1 > 0)
                 {
                     if (!VisitedCells[x, z - 1])
                     {
                         movement[movement.Count - 1].Push(new Vector2(temp.x, temp.y - 1));
                     }
                 }
             }
             if (grid[x, z, 3] == false)
             {
                 if (x - 1 > 0)
                 {
                     if (!VisitedCells[x - 1, z])
                     {
                         movement[movement.Count - 1].Push(new Vector2 (temp.x - 1, temp.y));
                     }
                 }
             }
             if (movement[movement.Count - 1].Count == 0 & movement.Count != 1)
             {
                 movement.Remove(movement[movement.Count - 1]);
             }
             PathFinder(movement, grid, PLoci, VisitedCells);
         }
     }
 
     private void Update()
     {
         if (Alert == true)
         {
             grid = Maindata.Grid;
             //Get cell location of player
             PLoci = new Vector2(((int)(player.transform.position.x) - ((int)player.transform.position.x % 5)) / 10, ((int)(player.transform.position.z) - ((int)player.transform.position.z % 5)) / 10);
             VisitedCells = new bool[Maindata.Size, Maindata.Size];
             for (int x = 0; x < Maindata.Size; x += 1)
             {
                 for (int z = 0; z < Maindata.Size; z += 1)
                 {
                     VisitedCells[x, z] = false;
                 }
             }
             var movement = new List<Stack<Vector2>>();
             var CLoci = new Vector2(((int)(transform.position.x) - ((int)transform.position.x % 5)) / 10, ((int)(transform.position.z) - ((int)transform.position.z % 5)) / 10);
             movement.Add(new Stack<Vector2>());
             movement[0].Push(CLoci);
             PathFinder(movement, grid, PLoci, VisitedCells);
             for (int x = 0; x < movement.Count; x += 1)
             {
                 Debug.Log(movement[x].Peek());
             }
             Alert = false;
         }
     }
 }
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

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

Random.Range throws StackOverFlow exception in recursion 1 Answer

Stack pop and push for unityscript? 0 Answers

Stack Overflow Exception on recursive functions 1 Answer

How can I fix UnityEngine.Random.Range (Int32 min, Int32 max) Stack Overflow 0 Answers

[Solved] Recursive Method causing Stack Overflow 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