Hi i'm programming a game and just now i got a stack overflow error but i encountered this error for the first time, i tried to google but i couldn't find a solution, i hope you can help me. Here is the code: Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using DG.Tweening; public class BattleUnit : MonoBehaviour { [SerializeField] bool isPlayerUnit; [SerializeField] BattleHud hud; public bool IsPlayerUnit { get { return IsPlayerUnit; } } public BattleHud Hud { get { return hud; } } public Pokémon Pokémon { get; set; } Image image; Vector3 originalPos; Color originalColor; private void Awake() { image = GetComponent<Image>(); originalPos = image.transform.localPosition; originalColor = image.color; } public void SetUp(Pokémon pokémon) { Pokémon = pokémon; if (isPlayerUnit) image.sprite = Pokémon.Base.BackSprite; else image.sprite = Pokémon.Base.FrontSprite; hud.SetData(pokémon); image.color = originalColor; PlayerEnterAnimation(); } public void PlayerEnterAnimation() { if (isPlayerUnit) image.transform.localPosition = new Vector3(-500f, originalPos.y); else image.transform.localPosition = new Vector3(500f, originalPos.y); image.transform.DOLocalMoveX(originalPos.x, 1f); } public void PlayAttackAnimation() { var sequence = DOTween.Sequence(); if (isPlayerUnit) sequence.Append(image.transform.DOLocalMoveX(originalPos.x + 50f, 0.25f)); else sequence.Append(image.transform.DOLocalMoveX(originalPos.x - 50f, 0.25f)); sequence.Append(image.transform.DOLocalMoveX(originalPos.x, 0.25f)); } public void PlayHitAnimation() { var sequence = DOTween.Sequence(); sequence.Append(image.DOColor(Color.gray, 0.1f)); sequence.Append(image.DOColor(originalColor, 0.1f)); } public void PlayFaintAnimation() { var sequence = DOTween.Sequence(); sequence.Append(image.transform.DOLocalMoveY(originalPos.y - 150f, 0.2f)); sequence.Join(image.DOFade(0f, 0.5f)); } }
BAM! Look carefully at what you're returning. This is why it's better to generally avoid all this property-and-backing-store noise unless you are doing it because it adds actual legitimate functionality, rather than just making CIS teachers smile.
The stack keeps track of the functions you've called. Each function gets a stack frame to store stuff like "what are my local variables?" and "where do we go when I'm done?" If you call a ton of functions in a row, without any of them ever getting a chance to finish running, you'll run out of space on the stack. This cause you to "overflow" the region of memory used to hold the stack. Hence, you get a "stack overflow". This usually happens because of infinite recursion: a function calling itself without ever hitting a stopping point. Properties are just fancy syntax for functions. So, a property that returns itself is just a function that calls itself!