Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Suppress errors in build?

Discussion in 'Scripting' started by mbaske, Aug 26, 2019.

  1. mbaske

    mbaske

    Joined:
    Dec 31, 2017
    Posts:
    473
    Hi - How can I suppress or ignore a "Skipped updating the transform of this Rigidbody because its components are infinite." error in a build?

    A little background: I'm training a machine learning agent to control lots of joints. The initial randomness of the agent's actions can cause the joint physics to "break". I've already tried to reduce the problem as much as I can using interpolation and by increasing solver iterations, but occasional errors still occur.
    This isn't a problem in the editor's game mode. My code checks the rigidbodies for NaN values, resets them if needed and things keep working, error or not. However, I'd like to train with a build rather than in the editor. As it turns out, this type of error freezes my build (Windows).

    Why would that error break code execution in the build but not in the edior, and what can I do to fix the issue? Thanks!
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    When code can throw an exception that you know can/ should happen eventually at runtime by design (imagine network code, where the connection may fail for reasons you cant influence), you can manually decide what to do in case of an exception, using the try-catch block:
    Code (CSharp):
    1. try{
    2.     // some code that may throw an ExampleException
    3. } catch(ExampleException e){
    4.     // whatever you want to do now. Print the error code, do nothing, fix your error (NaNs), ...
    5.     // either way, you caught the exception so the programm should not crash
    6. }
    This way you can "ignore" the exception and handle it yourself. As i implied above, you may even want to use the catch block to fix your NaN values in the first place, instead of checking for NaNs every iteration.
     
    Last edited: Aug 26, 2019
  3. mbaske

    mbaske

    Joined:
    Dec 31, 2017
    Posts:
    473
    Code (CSharp):
    1. try
    2. {
    3.     // code setting joint rotation & position
    4. }
    5. catch (System.Exception e)
    6. {
    7.     Debug.Log(e.ToString());
    8. }
    Hm, try-catch doesn't seem to catch this particular error. I've tried putting it at the individual joint level as well as on the top level which controls all joints. Also, the editor console only displays "Skipped updating the transform of this Rigidbody because its components are infinite. Could you have applied infinite forces, acceleration or set huge velocity?" - but doesn't provide a stack trace telling me where excatly the error occurred.
    Is it possible this error happens as a result of some invalid joint settings, but not immediately when those settings are changed? If the error is thrown by the physics engine at some later point than when the code actually executes, how would I catch it then?
     
  4. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    Hmm, i'm not sure on that. Generally code should always execute sequentially unless multithreading is involved.

    Maybe you can work around this error by setting an upper limit on the values you apply? After all the error suggests you are using infinite forces, acceleration or velocity at some point. And NaN and infinity are represented differently if i remember correctly.
     
  5. mbaske

    mbaske

    Joined:
    Dec 31, 2017
    Posts:
    473
    Thanks @Yoreki - I've encountered this issue a couple of times with joints. Apparently, forces become too large when a joint's rotation is changed drastically, causing the connected rigidbody to slam into another one. Which can result in NaN values for the rigidbody's position. Maybe this should better be a physics thread...

    Anyway, I hope someone has an idea how to handle these kinds of physics errors in C#, if at all possible.
     
  6. charmandermon

    charmandermon

    Joined:
    Dec 4, 2011
    Posts:
    352
    I've been chasing a NASTY crashing bug for the last week in editor and runtime and I think all of it can be explained by this: "Apparently, forces become too large when a joint's rotation is changed drastically, causing the connected rigidbody to slam into another one. Which can result in NaN values for the rigidbody's position. Maybe this should better be a physics thread..."

    Wow I think you just solved my issue...
     
  7. MUG806

    MUG806

    Joined:
    Jul 20, 2014
    Posts:
    4
    @charmandermon how did you solve this? I have the exact same problem killing my project.