Search Unity

Question The requested operation caused a stack overflow

Discussion in 'Documentation' started by Kalo2021, May 8, 2022.

  1. Kalo2021

    Kalo2021

    Joined:
    Jan 7, 2021
    Posts:
    2
    I'm pretty new to code and there's a error It says the requested operation caused a stack overflow. and I don't know why I think it's because I put this script on a bunch of things and unity cant handle it but i really don't know.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Dear : MonoBehaviour
    6. {
    7.     [Header("Sences")]
    8.     public int Direction;
    9.     public int State = 0;
    10.     public bool Hungry = false;
    11.     public float FoodSearchRadus = 10f;
    12.     public Collider2D[] Object;
    13.     public Plant plant;
    14.     public Vector2 Pos;
    15.     public bool FuncionCalled;
    16.     int areaSearch = 1;
    17.     public bool Moving = true;
    18.     //0 Normal
    19.     //1 fear
    20.     //2 panic
    21.     //3 GettingFood
    22.     [Header("Stats")]
    23.     public float Speed = 1;
    24.     public float SpeedS1 = 1;
    25.     public float SpeedS2 = 2;
    26.     public float SpeedS3 = 3;
    27.     public float Energy = 100f;
    28.     public float EnergyDrainSpeed = 1f;
    29.     public float EnergyDrainUnity = 5f;
    30.     // Start is called before the first frame update
    31.     void Start()
    32.     {
    33.         StartCoroutine("Dirtion");
    34.         StartCoroutine("Needs");
    35.  
    36.         Direction = Random.Range(1, 8);
    37.     }
    38.     void OnDrawGizmosSelected()
    39.     {
    40.         Gizmos.color = Color.green;
    41.         Gizmos.DrawWireSphere(transform.position, FoodSearchRadus);
    42.     }
    43.  
    44.      void OnCollisionEnter2D(Collision2D collision)
    45.     {
    46.             if (collision.gameObject.tag == ("Plant"))
    47.             {
    48.                 plant = collision.gameObject.GetComponent<Plant>();
    49.                 Energy += plant.Nutrence;
    50.                 FuncionCalled = false;
    51.                 Destroy(collision.gameObject);
    52.                 FuncionCalled = false;
    53.             Moving = true;
    54.             }      
    55.     }
    56.  
    57.  
    58.  
    59.     // Update is called once per frame
    60.     void FixedUpdate()
    61.     {
    62.  
    63.         if (Energy <= 60)
    64.         {            Hungry = true;
    65.            if (FuncionCalled == false)
    66.             {
    67.                 Object = Physics2D.OverlapCircleAll(transform.position, FoodSearchRadus);
    68.                 if(Object != null)
    69.                 {
    70.                     if (Object[0].gameObject.name == ("Dear"))
    71.                     {
    72.                         areaSearch = 1;
    73.                     }
    74.                     else
    75.                     {
    76.                         areaSearch = 0;
    77.                     }
    78.  
    79.                 }
    80.  
    81.             }
    82.             int Coliders = 0;
    83.            foreach(Collider2D collide in Object)
    84.             {
    85.                 Coliders++;
    86.             }
    87.  
    88.            if(Coliders >= 2)
    89.             {
    90.                 if (Object[0 + areaSearch] == null)
    91.                 {
    92.                     areaSearch++;
    93.                 }
    94.                 if (Object[0 + areaSearch].gameObject.CompareTag("Plant") && Object[0 + areaSearch] != null)
    95.                {
    96.                       FuncionCalled = true;
    97.                         if (Hungry == true)
    98.                         {
    99.                             State = 3;
    100.                             if (State == 3)
    101.                             {
    102.                                 Moving = false;
    103.                                 Vector2 Pos = Object[0 + areaSearch].transform.position - gameObject.transform.position;
    104.                                 transform.Translate(Pos * Speed * Time.deltaTime);
    105.  
    106.                             }
    107.                         }
    108.  
    109.                 }
    110.                
    111.             }
    112.            if(Coliders <= 1)
    113.             {
    114.                 State = 1;
    115.             }
    116.         }
    117.         else if(Energy > 60)
    118.         {
    119.             Hungry = false;
    120.             State = 0;
    121.         }
    122.  
    123.         if (Energy <= 0)
    124.         {
    125.             Destroy(gameObject);
    126.         }
    127.  
    128.         if(transform.position.y >= 5)
    129.         {
    130.             Direction = 4;
    131.         }
    132.  
    133.         if(transform.position.y <= -5)
    134.         {
    135.             Direction = 1;
    136.         }
    137.  
    138.         if(transform.position.x >= 10)
    139.         {
    140.             Direction = 3;
    141.         }
    142.  
    143.         if(transform.position.x <= -10)
    144.         {
    145.             Direction = 2;
    146.         }
    147.  
    148.         if(Direction > 8)
    149.         {
    150.             Direction = 1;
    151.         }
    152.  
    153.         if(Direction < 1)
    154.         {
    155.             Direction = 8;
    156.         }
    157.         //--------------------------------------------------------------------------------------------------------------
    158.  
    159.         if(Moving == true)
    160.         {
    161.             if (Direction == 1)
    162.             {
    163.                 transform.Translate(Vector2.up * Speed * Time.deltaTime);
    164.             }
    165.             else if (Direction == 2)
    166.             {
    167.                 transform.Translate(Vector2.right * Speed * Time.deltaTime);
    168.             }
    169.             else if (Direction == 3)
    170.             {
    171.                 transform.Translate(Vector2.left * Speed * Time.deltaTime);
    172.             }
    173.             else if (Direction == 4)
    174.             {
    175.                 transform.Translate(Vector2.down * Speed * Time.deltaTime);
    176.             }
    177.             if (Direction == 5)
    178.             {
    179.                 transform.Translate(Vector2.up * Speed * Time.deltaTime);
    180.                 transform.Translate(Vector2.right * Speed * Time.deltaTime);
    181.             }
    182.             else if (Direction == 6)
    183.             {
    184.                 transform.Translate(Vector2.left * Speed * Time.deltaTime);
    185.                 transform.Translate(Vector2.up * Speed * Time.deltaTime);
    186.             }
    187.             else if (Direction == 7)
    188.             {
    189.                 transform.Translate(Vector2.down * Speed * Time.deltaTime);
    190.                 transform.Translate(Vector2.left * Speed * Time.deltaTime);
    191.  
    192.             }
    193.             else if (Direction == 8)
    194.             {
    195.                 transform.Translate(Vector2.down * Speed * Time.deltaTime);
    196.                 transform.Translate(Vector2.down * Speed * Time.deltaTime);
    197.             }
    198.         }
    199.        
    200.  
    201.         if (State == 0)
    202.         {
    203.  
    204.             Speed = SpeedS1;
    205.  
    206.             EnergyDrainUnity = 1.5f;
    207.             EnergyDrainSpeed = 1;
    208.  
    209.         }
    210.         else if (State == 1)
    211.         {
    212.             Speed = SpeedS2;
    213.  
    214.  
    215.             EnergyDrainUnity = 2;
    216.             EnergyDrainSpeed = 1.5f;
    217.         }
    218.         else if (State == 2)
    219.         {
    220.             Speed = SpeedS3;
    221.  
    222.  
    223.             EnergyDrainUnity = 3;
    224.             EnergyDrainSpeed = 2;
    225.         }
    226.     }
    227.  
    228.      public void Reapte1()
    229.     {
    230.         StartCoroutine("Dirtion");
    231.     }
    232.     public void Reapte2()
    233.     {
    234.         StartCoroutine("Needs");
    235.     }
    236.  
    237.     IEnumerator Dirtion()
    238.     {
    239.  
    240.             if (State == 1)
    241.             {
    242.                 float time = Random.Range(.5f, 5f);
    243.                 yield return new WaitForSeconds(time);
    244.                 Direction = Random.Range(1, 8);
    245.  
    246.             }
    247.             else if (State == 0)
    248.             {
    249.                 float time = Random.Range(1, 8f);
    250.                 yield return new WaitForSeconds(time);
    251.                 Direction = Random.Range(1, 8);
    252.             Debug.Log("changed");
    253.             }
    254.             else if (State == 2)
    255.             {
    256.                 float time = Random.Range(.25f, 3.5f);
    257.                 yield return new WaitForSeconds(time);
    258.                 Direction = Random.Range(1, 8);
    259.             }
    260.             Reapte1();
    261.     }
    262.  
    263.     IEnumerator Needs()
    264.     {
    265.         yield return new WaitForSeconds(3f);
    266.  
    267.         float EnergySubract = Random.Range(EnergyDrainUnity * EnergyDrainSpeed, EnergyDrainUnity * EnergyDrainSpeed + 5);
    268.         Energy -= EnergySubract;
    269.     }
    270.  
    271. }
    272.  
     
  2. Kalo2021

    Kalo2021

    Joined:
    Jan 7, 2021
    Posts:
    2
    Note: sorry if the code is unclear and messy