Home > Enterprise >  Whenever I call this function Unity doesn't respond anymore
Whenever I call this function Unity doesn't respond anymore

Time:12-19

I always have to close Unity through Task-Manager if I call this function through a button, what is wrong with my code? I know there are is a lot of processing required for this code but the process never finishes:

public void SudokuLösen()
    {
        for(int p = 1; p < 81; p  )
        {
            if(PositionWert[p] != 0){
            for(int z = 1; z < 9; z  )
            {
                if(PositionWert[p] == z)
                {
                    int Reihe = 0;
                    int Spalte = 0;
                    int Platz = 0;
                    for(int m = 0; m < 8; m  )
                    {
                        for(int r = 1; r < 9; r  )
                        {
                            if(p - m * 9 == r)
                            {
                                Reihe = r;
                            }
                        }
                    }
                    for(int s = 0; s < 9; s  )
                    {
                        if(p == Reihe * 9 - 9   s )
                        {
                            s = Spalte;
                        }
                    }
                    for(int g = -8; g < 8; g  )
                    {
                        if((Spalte   g > 0) && (Spalte   g < 10))
                        {
                            Blockiert[(p   z * 81 - 81)   g] = true;
                        }
                        if((Reihe   g > 0) && (Reihe   g < 10))
                        {
                            Blockiert[(p   z * 81 - 81)   g * 9] = true;
                        }
                    }
                    for(int vx = 1; vx < 3; vx  )
                    {
                        if((Spalte == 0   vx) || (Spalte == 3   vx) || (Spalte == 6   vx))
                        {
                            Platz  = vx;
                        }
                        if((Reihe == 0   vx) || (Reihe == 3   vx) || (Reihe == 6   vx))
                        {
                            Platz  = (3 * vx - 3);
                        }
                    }
                    for(int q = 1; q < 4; q  )
                    {
                        Blockiert[(p   z * 81 - 81)   Quadrat[Platz * 4 - 4   q]] = true;
                    }
                }
            }
            }
        }
    }
}

the Arrays are defined like this:

public static bool[] Blockiert = new bool[730];
public static int[] PositionWert = new int[82];
private int[] Quadrat = {0, 10, 11, 19, 20, 8, 10, 17, 19, 7, 8, 16, 17, -8, -7, 10, 11, -10, -8, 8, 10, -11, -10, 7, 8, -17, -16, -8, -7, -19, -17, -10, -8, -20, -19, -11, -10};

I thought that it might just be because my computer would just take forever to call this function so I waited a long time and saw that Unity nearly used 15% of my CPU so it didn't really do much and the function processing just doesn't finish.

Does anyone know what is wrong or what I should change?

CodePudding user response:

A freeze usually means you have a never ending loop somewhere.

You have

for(int s = 0; s < 9; s  )
{
    if(p == Reihe * 9 - 9   s )
    {
        s = Spalte;
    }
}

where you reassign s = 0 if a certain condition is met.

This condition will then be met in each future iteration as well since none of the other parameters change within that loop => never ending loop.

Should it maybe rather be

Spalte = s;

?

CodePudding user response:

If u want to check whether the code still running, u can print messages in console. but this won't work for u since Debug.Log will be printed after each loop of lifecycle. so print to log file would be a option, or like comment says, use breakpoint.

since this could be a huge method for unity that can't be solved in one frame, using Coroutine would be a nice option to distribute the operation into more frames. check this out to find more info.

btw, using german or other non-ascii letter in function name may not a good idea.

  • Related