• 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 evilbluesphere · Nov 27, 2016 at 07:55 PM · javascriptrecursionoverflow

Stack overflow on finite recursion

I tried to make an algorithm to fill all tiles adjacent to each other with the same color, but when I run it it throws the Exception.

This is the code:

 function Colorear(naciones:Transform[],colores:Color[],n:int,color:Color){
     if(colores[n].a==0){
         colores[n]=color;
         for(var nacion_vecina:Transform in naciones[n].GetComponent(nation1).locacion.GetComponent(ubicacion).Conexiones){
             var lugar=System.Array.IndexOf(naciones,nacion_vecina.GetComponent(nation_helper1).nation);
             if(nacion_vecina.GetComponent(nation_helper1).nation!=null){
                 if(nacion_vecina.GetComponent(nation_helper1).nation.GetComponent(nation1).colornacion==naciones[n].GetComponent(nation1).colornacion && colores[lugar].a==0){
                     Colorear(naciones,colores,lugar,colores[n]);
                 }
             }
                 
         }
     }
     
 }

Since the amount of nations is finite, and this code colors them each time it's called before making the recursion, it should eventually end (they are only a few hundred nations), and yet it crashes.

Comment
Add comment · Show 1
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 Sergio7888 · Nov 27, 2016 at 07:58 PM 0
Share

You are calling Colorear inside a loop inside Colorear, and because this Colorear is called multiple times and called more multiple times again, again, again and became called infinite times.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How get position from a game object? 5 Answers

Help converting a few lines of JS to C#. 1 Answer

Js to C# conversion without error but can't work 2 Answers

How to implement onTriggerEnter and onTriggerExit in Vuforia. 0 Answers

Disabling random GUI Button from array 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges