Search Unity

Resolved Stack overflow

Discussion in 'Scripting' started by GameDev_A, Jan 23, 2023.

  1. GameDev_A

    GameDev_A

    Joined:
    Sep 5, 2022
    Posts:
    43
    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):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. using DG.Tweening;
    7.  
    8. public class BattleUnit : MonoBehaviour
    9. {
    10.     [SerializeField] bool isPlayerUnit;
    11.     [SerializeField] BattleHud hud;
    12.  
    13.     public bool IsPlayerUnit
    14.     {
    15.         get { return IsPlayerUnit; }
    16.     }
    17.  
    18.     public BattleHud Hud
    19.     {
    20.         get { return hud; }
    21.     }
    22.  
    23.     public Pokémon Pokémon { get; set; }
    24.  
    25.     Image image;
    26.     Vector3 originalPos;
    27.     Color originalColor;
    28.     private void Awake()
    29.     {
    30.         image = GetComponent<Image>();
    31.         originalPos = image.transform.localPosition;
    32.         originalColor = image.color;
    33.     }
    34.  
    35.     public void SetUp(Pokémon pokémon)
    36.     {
    37.         Pokémon = pokémon;
    38.         if (isPlayerUnit)
    39.             image.sprite = Pokémon.Base.BackSprite;
    40.         else
    41.             image.sprite = Pokémon.Base.FrontSprite;
    42.  
    43.         hud.SetData(pokémon);
    44.  
    45.         image.color = originalColor;
    46.         PlayerEnterAnimation();
    47.     }
    48.  
    49.     public void PlayerEnterAnimation()
    50.     {
    51.         if (isPlayerUnit)
    52.             image.transform.localPosition = new Vector3(-500f, originalPos.y);
    53.         else
    54.             image.transform.localPosition = new Vector3(500f, originalPos.y);
    55.  
    56.         image.transform.DOLocalMoveX(originalPos.x, 1f);
    57.     }
    58.  
    59.     public void PlayAttackAnimation()
    60.     {
    61.         var sequence = DOTween.Sequence();
    62.         if (isPlayerUnit)
    63.             sequence.Append(image.transform.DOLocalMoveX(originalPos.x + 50f, 0.25f));
    64.         else
    65.             sequence.Append(image.transform.DOLocalMoveX(originalPos.x - 50f, 0.25f));
    66.  
    67.         sequence.Append(image.transform.DOLocalMoveX(originalPos.x, 0.25f));
    68.     }
    69.  
    70.     public void PlayHitAnimation()
    71.     {
    72.         var sequence = DOTween.Sequence();
    73.         sequence.Append(image.DOColor(Color.gray, 0.1f));
    74.         sequence.Append(image.DOColor(originalColor, 0.1f));
    75.     }
    76.  
    77.     public void PlayFaintAnimation()
    78.     {
    79.         var sequence = DOTween.Sequence();
    80.         sequence.Append(image.transform.DOLocalMoveY(originalPos.y - 150f, 0.2f));
    81.         sequence.Join(image.DOFade(0f, 0.5f));
    82.     }
    83. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,686
    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.
     
    Bunny83 likes this.
  3. GameDev_A

    GameDev_A

    Joined:
    Sep 5, 2022
    Posts:
    43
    Ok thanks
     
  4. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    720
    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!
     
    Bunny83 and GameDev_A like this.
  5. GameDev_A

    GameDev_A

    Joined:
    Sep 5, 2022
    Posts:
    43
    Thanks a lot for the explanation, I'll correct the code tomorrow