Search Unity

Question Unity freezes on play after coming back from script modifications

Discussion in 'Scripting' started by capocchione, Jul 1, 2020.

  1. capocchione

    capocchione

    Joined:
    Dec 9, 2019
    Posts:
    47
    I've made some modifications to my scripts, checked they are not working, came back to original versions and... Unity freezes on play.
    I don't know what is happening because the scripts are the same as they worked before modifications.
    I tried to attach to unity the visual studio code debugger and MAYBE I've isolated the problem.

    the equation.cs calculates some values every update, here is the original working code:


    Code (CSharp):
    1. using UnityEngine;
    2. public class Equation : MonoBehaviour
    3. {
    4.     static SetupNew setup;
    5.     static float wExt;
    6.     static float k;
    7.     static float maxInternodeLenght;
    8.     TreeGeneticInfo treeInfo;
    9.     static float maxTreeHeight;
    10.     static GameObject go;
    11.  
    12.     public void Init()
    13.     {
    14.         setup = GameObject.Find("Setup").GetComponent<SetupNew>();
    15.         treeInfo = setup.StaticGeneticInfo;
    16.         k = setup.K;
    17.         wExt = setup.WExt;
    18.         maxInternodeLenght = treeInfo.MaxInternodeLenght;
    19.         maxTreeHeight = treeInfo.MaxTreeHeight;
    20.         go = gameObject;
    21.  
    22.     }
    23.  
    24.     public static float equation(float t, float annualGrowth)
    25.     {
    26.         float y_prime;
    27.        
    28.         float totalGrowthFactor = 1 - (setup.Manager.TotalLenght() / maxTreeHeight);
    29.                
    30.         y_prime = k * annualGrowth * (1 - (annualGrowth / maxInternodeLenght)) * totalGrowthFactor;
    31.        
    32.         return y_prime;
    33.     }
    34. }
    Now, I've set debugger stop points here in the code:



    What I notice from debugger is the "infinite loop" between the two stopping points (but I'm not sure if I got it right)
    As I mentioned before, the code worked just fine and I don't know what happened. Yes, I've modified something but I'm sure I'm back to the original working situation.
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    When you say "infinite loop between the two stopping points", do you mean that it keeps going back and forth between breaking on one and breaking on the other, or do you mean that you reach the first breakpoint and then Unity freezes before you reach the second?

    In the first case, what's the code that's calling equation()? (Is it always being called from the same place? In the debugger, you should be able to see the call stack.)

    In the second case, what's the code for TotalLength()?
     
    capocchione likes this.
  3. capocchione

    capocchione

    Joined:
    Dec 9, 2019
    Posts:
    47
    it keeps going back and forth between breaking on one and breaking on the other

    First breaking point:

    Second break point:


    the equation is called from another script:
    annualLengthGrowth = Runge.runge(0.0f, 1.0f, nextValue, .1f, new Runge.Function(Equation.equation));


    meaning that annualLenghtGrowth is calculated as ODE solution of the equation using RungeKutta numerical method (another script) with parameters in parenthesis (start value, final value, initial value, time step)
     
    Last edited: Jul 1, 2020
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Let me put that another way:

    If you keep hitting those breakpoints, that meant equation() is being called over and over. So move your entire analysis out of this function and up a level on the callstack, so whatever is doing the calling over and over.

    Either that thing is, itself, being called over and over (in which case, keep going up) or it's got some kind of loop, or recursion, or other structure that causes the call to happen more than once. That's what you're looking for.

    Looking at the source code of equation() or breaking inside it in the debugger is useless. You're too deep to see the problem. Back up.
     
    capocchione likes this.
  5. capocchione

    capocchione

    Joined:
    Dec 9, 2019
    Posts:
    47
    Solved, as you suggested, I started to investigate from the very beginning (Awake() functions), then going deeper (Start() and Update()) and I finally found the error: a "wild" cast converting a float value to int. The equation doesn't want a int as argument so the freeze.

    Thank you for helping!
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    That would cause a compiler error, I don't see how it would cause a freeze?
     
  7. capocchione

    capocchione

    Joined:
    Dec 9, 2019
    Posts:
    47
    I don't know. For "freeze" I meant that when i press play in unity, the program stucks and I need to force close it. Like when an infinite loop occurs