- Home /
Recursive properties crash Unity
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.
[SerializeField]
private float _ammo = 10;
public float ammo
{
get {
return ammo; // this should be _ammo, which makes it recursive now
}
set {
_ammo = value;
}
}
Is there some way this can be catched beforehand?
Answer by Dave-Carlile · Feb 19, 2016 at 01:50 PM
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".
Answer by Revolter · Aug 13, 2021 at 04:52 AM
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
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
An OS design issue: File types associated with their appropriate programs 1 Answer
Having trouble swapping GameObjects on button click 1 Answer
Advantages of properties? 2 Answers