I keep crashing Unity - but only randomly! - Can anybody help?

Question

Hi guys. I’ve never posted here before asking for actual, specific coding advice. I hope it’s okay if I ask here!

I’m delving into the wacky realm of procedurally generated content. In this case, I’m starting off by trying to recreate a Legend of Zelda-for-NES-esque dungeon crawler where the game generates a series of individual rooms and connects them together. I haven’t gotten to the part where I connect the rooms yet, but so far I’m just trying to make it so it creates the layout of the dungeon but excludes one specific direction. The idea here is if the player character walked through the entrance of the dungeon, there won’t be any rooms generated behind the dungeon entrance (because that wouldn’t make sense.) It’s a pretty trivial detail, but it’s something I want to accomplish just to say I did it.

So far, I got it working, most of the time! It creates a starter room (a white square) and then a randomized map of blue square rooms, followed by a green square for the final room. No rooms are ever created “behind” the white room.

My only problem is the game randomly freezes up Unity. I haven’t been able to get any Console commands to execute before the lock-up yet so far, but I’m really confused as to how this is happening. Here’s a link to my script:

https://pastebin.com/gixWZKfn

I hope it makes sense! I’ve found the problem started happening when I set up that while loop in the Start() method, once I started assigning the lastPos Vector3. I did find that when I assign a Vector3.zero to lastPos upon declaration, Unity CONSISTENTLY freezes up, as opposed to only sometimes.

Any idea what I’m doing wrong?

Posted Jun 15, 2022, 12:14 AM UTC

15
melvmayUnity Technologies•

Nearly always the candidate for freezing is a while loop and I see one in your code.

If you never provide an exit condition then you'll never escape it. I'd say for sanity, I'd only check N times by adding in a counter and if it exceeds a threshold then break out of the while loop. Even if you only use this for debugging.

2
pmurph0305•

I would either attach the debugger from vs code so you can step through to locate why your infinite loop is happening. Or less preferably add debug statements and after it freezes/crashes use the editor's log file to figure out why.

1
FlashDansonOPBeginner•

I’m new to this, how do I do the first thing you proposed?

1
melvmayUnity Technologies•

Typing "unity debugging" in Google gives you the following link: https://docs.unity3d.com/Manual/ManagedCodeDebugging.html

0
koyima•

You have probably made a loop that never ends

Don't forget things in start are supposed to finish, if you want things to be active throught the duration of the game making a while loop in start isn't the way

1