Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Unity Has Started Crashing because of a EnemyController script using NavMesh

Discussion in 'Scripting' started by unity_F3F23D39B27A5B4AB766, Apr 17, 2024.

  1. unity_F3F23D39B27A5B4AB766

    unity_F3F23D39B27A5B4AB766

    Joined:
    Mar 20, 2024
    Posts:
    5
    Hello everyone, i have no idea why this has started happening, it literally happened overnight and i DID NOT touch the script yesterday, this morning when i opened my project and started testing stuff It started displaying errors such as:
    - StackOverFlowException: The requested operation caused a stack overflow.
    - TLS Allocator ALLOC_TEMP_THREAD, underlying allocator ALLOC_TEMP_THREAD has unfreed allocations, size 470

    the only thing i added now was a bunch of debug logs to find out at wich point it crashes, and i think
    there could be a problem in the ChangeZombiStatus Method.

    I can provide the other scripts linked to EnemyController if needed.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5.  
    6. public class EnemyController : MonoBehaviour
    7. {
    8.     public enum EnemyState
    9.     {
    10.         Locked, Idle, Locomotion, Reaction, Action, Death
    11.     }
    12.  
    13.     public EnemyState startingStatus;
    14.     public EnemyState currentStatus;
    15.  
    16.  
    17.     private EnemyReaction _reaction;
    18.     private EnemyLocomotion _locomotion;
    19.     private EnemyAction _action;
    20.     private Animator _animator;
    21.     private NavMeshAgent _agent;
    22.  
    23.     private EnemyStatus currentZombieStatus;
    24.     public PlayerController _player { get; set; }
    25.     public float Chaserange = 10;
    26.  
    27.     private void Awake()
    28.     {
    29.  
    30.         _animator = GetComponentInChildren<Animator>();
    31.         _agent = GetComponent<NavMeshAgent>();
    32.  
    33.         _locomotion = GetComponent<EnemyLocomotion>();
    34.         _locomotion.Init(this);
    35.  
    36.         _reaction = GetComponent<EnemyReaction>();
    37.         _reaction.Init(this);
    38.  
    39.         _action = GetComponent<EnemyAction>();
    40.         _action.Init(this);
    41.     }
    42.  
    43.     private void OnEnable()
    44.     {
    45.         _player = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController>();
    46.      
    47.  
    48.     }
    49.  
    50.     private void OnDisable()
    51.     {
    52.         _player = null;
    53.        
    54.     }
    55.  
    56.     private void Start()
    57.     {
    58.         _player = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController>();
    59.         ChangeZombieStatus(startingStatus);
    60.        
    61.     }
    62.  
    63.     private void Update()
    64.     {
    65.         ExecuteStatus();
    66.  
    67.         //_animator.SetBool("on_ground", !_agent.isOnOffMeshLink);
    68.         _animator.SetFloat("speed", _agent.velocity.magnitude);
    69.        
    70.     }
    71.  
    72.  
    73.     public void LockZombie()
    74.     {
    75.         //Startingstatus = ZombieStatus.Locked;
    76.         ChangeZombieStatus(EnemyState.Locked);
    77.         //if(_chase != null)
    78.         //    _chase.enabled = false;
    79.         //if (_locomotion != null)
    80.         //_locomotion.enabled = false;
    81.        
    82.     }
    83.  
    84.  
    85.  
    86.     public void UnlockZombie()
    87.     {
    88.         //if (_chase != null)
    89.         //    _chase.enabled = true;
    90.         //if (_locomotion != null)
    91.         //    _locomotion.enabled = true;
    92.         ChangeZombieStatus(EnemyState.Idle);
    93.        
    94.     }
    95.  
    96.  
    97.  
    98.     public void ChangeZombieStatus(EnemyState newStatus)
    99.     {
    100.         //Here is where Unity Crashes i think
    101.        
    102.         if (currentStatus != newStatus)
    103.         {
    104.             if (currentZombieStatus != null)
    105.             {
    106.                 Debug.Log("Crash if CurrentStatus != null");
    107.                 currentZombieStatus.OnStatusExit();
    108.                 currentZombieStatus = null;
    109.             }
    110.  
    111.  
    112.             currentStatus = newStatus;
    113.  
    114.             _locomotion.enabled = false;
    115.             _reaction.enabled = false;
    116.             _action.enabled = false;
    117.  
    118.             switch (currentStatus)
    119.             {
    120.                
    121.                 case EnemyState.Locomotion:
    122.                     Debug.Log("Crash CurrentStatus Case Locomotion");
    123.                     _locomotion.enabled = true;
    124.                     currentZombieStatus = _locomotion;
    125.                     break;
    126.                 case EnemyState.Reaction:
    127.                     Debug.Log("Crash CurrentStatus Case Reaction");
    128.                     _reaction.enabled = true;
    129.                     currentZombieStatus = _reaction;
    130.                     break;
    131.                 case EnemyState.Action:
    132.                      //The crash might be here more spefically
    133.                     Debug.Log("Crash CurrentStatus Case Action");
    134.                     _action.enabled = true;
    135.                     currentZombieStatus = _action;
    136.                     break;
    137.                 default:
    138.                     Debug.Log("Crash CurrentStatus Case null");
    139.                     currentZombieStatus = null;
    140.                     break;
    141.  
    142.             }
    143.  
    144.             if (currentZombieStatus != null)
    145.             {
    146.                 //The crash might be here more spefically
    147.                 Debug.Log("Crash CurrentStatus");
    148.                 currentZombieStatus.enabled = true;
    149.                currentZombieStatus.OnStatusEnter();
    150.             }
    151.  
    152.         }
    153.     }
    154.  
    155.  
    156.     void ExecuteStatus()
    157.     {
    158.         Debug.Log("Crash ExecuteStatus");
    159.         if (currentStatus == EnemyState.Locked) return;
    160.  
    161.         if (currentStatus == EnemyState.Action)
    162.         {
    163.             return;
    164.         }
    165.  
    166.         if (_player)
    167.         {
    168.             float distance = Vector3.Distance(transform.position, _player.transform.position);
    169.             if (distance <= Chaserange)
    170.             {
    171.                 ChangeZombieStatus(EnemyState.Reaction);
    172.             }
    173.             else
    174.             {
    175.                 ChangeZombieStatus(EnemyState.Locomotion);
    176.             }
    177.  
    178.             return;
    179.         }
    180.  
    181.         if (currentStatus == EnemyState.Idle)
    182.         {
    183.             ChangeZombieStatus(EnemyState.Locomotion);
    184.         }
    185.  
    186.     }
    187. }
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    7,019
    This tells you where the stackoverflow occured. Just look at the provided callstack, and you'll see where (line numbers, most callstack entries should be hyperlinked). If not, it might be within Unity's own code but at least you'll see which of your methods (if any) made that call.
     
    MelvMay likes this.
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,701
    Also note then when talking about a "crash"; that typically means that the Unity Editor application closes and presents you with the bug reporter tool. What you're seeing here is C# reporting an exception thrown executing your script and presumably the Unity Editor application isn't exiting.

    An exception normally shows you where it was thrown i.e. the call-site where it was thrown and the whole callstack should be available in most cases.

    Attach a debugger and run it and it'll stop there.
     
    Kurt-Dekker likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,432
    I'll guess that you've somehow cross-linked your inter-object events (which I'm guessing get hooked up in your various Init(this) calls) into an endless loop.

    As Melv says, hook up the debugger and step through... most likely you are ping-ponging ceaselessly between two states, or repeated settings of a single state.

    By debugging you can find out exactly what your program is doing so you can fix it.

    https://docs.unity3d.com/Manual/ManagedCodeDebugging.html

    Use the above techniques to get the information you need in order to reason about what the problem is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.