Unity Freezes without error

Hello,

When I play my project, it runs for like a second, then freezes. Spent three evenings trying to figure out why, without progress. I realize I need some help.

Besides the default Main Camera and Directional Light, the scene contains only one game object.
7632889--950053--upload_2021-11-5_19-59-41.png

This object contains two C# scripts:
7632889--950068--upload_2021-11-5_20-16-46.png

Both are classes which inherits MonoBehavior and contains update functions.

The World class Update() function which looks as following:

void Update()
    {
        counter += 1;
        Debug.Log("World Update " + counter.ToString()) ;
        byte ix = (byte)Random.Range(0f, Region.width - 1);
        byte iy = (byte)Random.Range(0f, Region.height - 1);
        byte iz = (byte)Random.Range(0f, Region.width - 1);
        regions[0, 0].SetVoxel(ix, iy, iz, 0);
        CheckCurrentY();
        UpdateRegions();
        Debug.Log("World Update " + counter.ToString() + " Done!");
        }
     
    }

The scene plays fine without the line:

regions[0, 0].SetVoxel(ix, iy, iz, 0);

So you’d think I’d easily find the problem by stepping into the SetVoxel() function. Well, I tried that, but the freeze happens only after some unpredictable number of updates, so it’s difficult to enter it in the right moment. Moreover, by placing Debug.Log() messages on strategic places in my code I noted that the freeze doesn’t happen while executing that line. Instead, the last message shown is from the update function of the other class: Input Controller.
7632889--950077--upload_2021-11-5_20-35-29.png
And that update function is essentially empty:

void Update()
    {
        Debug.Log("InputController Update");
        Debug.Log("InputController Done.");
    }

So, the freeze doesn’t happens while executing either update function. Rather the editor freezes between them.

Does anyone have a clue what could be wrong, or suggestions how to proceed?

EDIT: Also, no, my code does not contain any while-loops.

Also: I’m kinda new to C# and Unity. My apologies if this is daft.

If Unity freezes, that’s usually because you have an infinite loop somewhere that it’s hitting.

1 Like

This ^ ^ ^

I would be looking very carefully at what lines 9, 10 and 11 do… just try commenting each one out and I bet the “crash” goes away. One of those has likely either an infinite or a very large loop in it.

Thanks!

Yeah, infinite loop was my first guess too. Good that you can confirm.

It was the fact that last log is always between the Update() functions rather than inside them that made me suspicious that something else might be happening. I thought maybe the infinite loop doesn’t occur in my own code, but rather in some overhead stuff that runs between. I.e. maybe my code messes up something that indirectly causes an infinite loop somewhere else?

Anyway, if you can share any useful strategy for finding the infinite loop, please share.
I tried adding comments to suspect for-loops to see where it got stuck, but so far no cigar.

Unless and until your function returns to Unity, 100% of all Debug.Log() statements you output during that frame will NOT be shown, so that’s not a useful way to isolate.

If you suspect loop termination logic, put some counters in that exit after reaching an unreasonable number of iterations and also print a Debug.LogError(). This will tell you that the loop exit logic is failing and also let you see where.

Thanks! I’ll try that method.

Clearly I need to learn more about Debug.LogError(), Debug.LogWarning(), and Debug.Log() and what sets them apart.

Solved this now somehow.

I don’t know exactly what caused the crash, but I started to suspect that using (byte) to define the range of for-loops is not good, as conversion is kind of weird, e.g. (byte)(-1) = 255.

So I changed all (byte) variables to (int) for now. After some trouble-shooting related to the change, the code was running as intended!

Finally I can move on to something more fun.

Thanks for the help!

1 Like

Just wait until you realize what this code does:

using UnityEngine;

public class Test : MonoBehaviour
{
    private int x = 2147483647;

    private void Start()
    {
        Debug.Log(x);
        x++;
        Debug.Log(x);
    }
}

Surprise! 7634938--950632--screenshot.png

All variables work like that. If you reach the maximum capacity it rolls over to the minimum value and if you reach the minimum value it rolls over to the maximum. In your case you used a byte which stores numbers between 0 and 255 inclusive. When you feed -1 into it, it becomes 255.

1 Like

WAT… I hope you filed a bug!!

Adding some technical backstory to this, it’s called two’s complement and it is used to represent signed numbers in an otherwise-unsigned array of bits, which if course is all your computer has, just bits, 0 and 1.

https://en.wikipedia.org/wiki/Two's_complement

Pay attention to that first three-bit table and how those same 3 bits can mean one thing when signed and an entire other thing when unsigned.

1 Like

Thanks for the education!
So yeah, I need to read more about datatypes.