Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Script Pausing Problem

Discussion in 'Scripting' started by tyler_shau, Sep 25, 2022.

  1. tyler_shau

    tyler_shau

    Joined:
    Feb 17, 2021
    Posts:
    27
    Hello,

    I am having a problem where I execute this one method that works as intended in the Editor, but when I leave its IOS App Version running for 24 hours and come back, it makes an error occur. The error is that it pauses the execution of the whole method, so every code line after it does not execute. Anyone know what type of error this is? Or what could be causing it?

    For the "Test" function, it will never print "2" to the console since the method pauses/stops.

    Code (CSharp):
    1. public string CalculateWoodPerMin(bool NoVehicleCap, bool noMoneyLabel) {
    2.         double total = 0d;
    3.         for (int i = 0; i <= LJ_Number - 1; i++) {
    4.             int distance = LJChoppingXCoords[(i - (8 * (int)Mathf.Floor(i / 8)))] - (595 + ((i - (5 * (int)Mathf.Floor(i / 5))) * 15));
    5.             float secondsToMoveDist = distance / (10 * Convert.ToSingle(UGMoveSpeed(i, 0)));
    6.             float secondsChopping = Convert.ToSingle((Convert.ToDouble(UGHoldingCap(i, false))) / MoneyMultiplier(i));
    7.             float secondsWaiting = Convert.ToSingle(Convert.ToDouble(UGHoldingCap(i, false)) / Convert.ToDouble(UGUnloadSpeed(i, false)));
    8.             double moneyPerSecLJ = (MoneyMultiplier(i) / (secondsToMoveDist + secondsChopping + secondsWaiting));
    9.             total += moneyPerSecLJ;
    10.         }
    11.         if (total > TD_Script.getTotalWoodPerMin() && !NoVehicleCap) {
    12.             total = TD_Script.getTotalWoodPerMin();
    13.         }
    14.         if (!NoVehicleCap) {
    15.             moneyPerSec = total;
    16.         }
    17.         else {
    18.             return total.ToString();
    19.         }
    20.         if (noMoneyLabel) {
    21.             return total.ToString();
    22.         }
    23.         else {
    24.             return NL_Script.moneyLabeler(total, false);
    25.         }
    26.     }
    27.  
    28. private void Test() {
    29.         Debug.Log("1");
    30.         CalculateWoodPerMin(false, true);
    31.         Debug.Log("2");
    32.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    29,658
    Remember: NOBODY here memorizes error codes and we cannot read your mind or see your computer screen.

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.

    You must find a way to get the information you need in order to reason about what the problem is.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    Debug.Log()
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    When in doubt, print it out!(tm)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.
     
  3. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    What is the exact error, what line is it on? Does this work after 1 minute? 5 minutes?
     
  4. tyler_shau

    tyler_shau

    Joined:
    Feb 17, 2021
    Posts:
    27
    Thanks for the info, but the error doesn't occur in Xcode as an App, or the Unity Editor. The error only occurs as an app that is in Testflight. Also, I'm mainly trying to figure out what type of error pauses a method. I think the error would be more easy to identify if I could know that.
     
  5. tyler_shau

    tyler_shau

    Joined:
    Feb 17, 2021
    Posts:
    27
    I don't know exactly what line it is on, but I know the one function that causes it, and it's the CalculateWoodPerMin() function. I'm trying to figure out what type of error causes the whole script to pause. I can't use Debug.Log because the error doesn't occur in the Unity Editor or XCode, it only occurs as a testflight app. I also figured out that the time it takes for the error to occur is very random, it can range from 20 minutes to a couple hours. It occurs after I log off the app, wait a couple hours, then log back on.
     
  6. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,380
    "Error" suggests that the code threw an error. Did it actually throw an error? Or is it just getting caught in an infinite loop. My money's on that your code has an infinite loop, but you're going to have to investigate since you didn't show all the code.

    Debug.Log does work, it creates an output file where all of the log goes to for builds.
    https://docs.unity3d.com/Manual/LogFiles.html
     
  7. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    If you can reproduce, you can view the device logs in XCode which will show your Debug.Log output https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ And you mentioned "after 24 hours", does it work at all?