I accidentally usd a recursive proprty which causes Unity to crash directly when referenced and screen went black for a couple of sseconds. Seeing the code I know I did this myself but can’t Unity catch this exception before runtime? Bellow is my code which causes Unity to crash instantly when I tried to set the field.
private float _ammo = 10;
public float ammo
return ammo; // this should be _ammo, which makes it recursive now
_ammo = value;
Is there some way this can be catched beforehand?
Recursion is a valid programming technique for certain problems. For example, I have a “Root” property that walks its way up a parent/child hierarchy until it find the node with a null parent. I have an exit condition - “parent == null” to stop the recursion.
How is Unity supposed to know you’re not intending to use recursion? It can’t, so if you recurse too much you end up getting a stack overflow during run time. Not much else you can do about it other than “don’t do that”.
It would be fine if you could identify the problematic place in code, but the Editor logs are useless in this case.
This is still reproducible in the latest version of Unity