Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Bug / Crash  Mixing 2D and 3D Physics results in Obi MissingReferenceException:
#1
The Problem

After upgrading to Obi Cloth 7.1, my project suddenly produced the error

"MissingReferenceException: The object of type 'ObiRigidbody2D' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object."

This error was happening everywhere and on every frame!


The Solution

My project mixes 2D and 3D physics on purpose. The cause of the error was this snippet of gameobject hierarchy

> Rigidbody 2D
  > Box Collider 2D
  ...
  > Box Collider (i.e. a 3D collider)
    Obi Collider (i.e. the 3D version of ObiCollider

Notice the 2D Unity Rigidbody mixed with the 3D Unity Box Collider and 3D Obi Collider. 

The fix is to insert a 3D Unity Rigidbody below the 2D Unity Rigidbody like

> Rigidbody 2D
  > Box Collider 2D
  ...

  > Rigidbody (i.e. Unity's 3D Rigidbody component) 
    Box Collider (i.e. a 3D collider)
    Obi Collider (i.e. the 3D version of ObiCollider

I also marked the (3D) Rigidbody as "Is Kinematic" so that the 2D Box Collider and the 3D Box Collider would behave as if they are part of the same Rigidbody (which is the behavior I'm looking for).


Explanation

In the editor, Obi thought that everything was fine with the original hierarchy. ObiCollider was happy because there was a 3D Unity Collider on the same gameobject that the ObiCollider was attached to. And at runtime, just as the online documentation says, Obi "look[ed] for the first Rigidbody component up its hierarchy and attach[ed] an ObiRigidbody component to it." Since the first Rigidbody component Obi encountered was the Rigidbody2D, Obi added an ObiRigidbody2D component to the hierarchy like this

> Rigidbody 2D 
  Obi Rigidbody 2D

  > Box Collider2D
  ...
  > Box Collider (i.e. a 3D collider)
    Obi Collider (i.e. the 3D version of ObiCollider



But then Obi threw the above MissingReferenceException. 

I traced the Obi code a bit. The error is thrown from ObiRigidbody2D.CacheVelocities() where it turns out that this is null!! I didn't know that was possible Sonrisa . I think it has something to do with parallelization. One thread deleted the ObiRigidbody2D (I'm not exactly sure where in the code the deletion happens), but there was still a reference to the obiRigidbody2D in memory that is able to be called??

At any rate, by removing more and more of the gameobjects in my scene I eventually whittled the object hierarchy down to the above original tree and noticed the mixing of 2D and 3D components and then remembered that it was an ObiRigidbody2D that was being deleted and throwing the error. On a whim, I modified the hierarchy to be:

> Rigidbody 2D
  > Box Collider 2D
  ...

  > Rigidbody (i.e. Unity's 3D Rigidbody component) 
    Box Collider (i.e. a 3D collider)
    Obi Collider (i.e. the 3D version of ObiCollider

At runtime, Obi now adds an ObiRigidbody(2D) to both the upper and the lower gameobject. But I guess that's just what is supposed to happen(?)

> Rigidbody2D
  ObiRigidBody2D
  > Box Collider 2D
  ...

  > Rigidbody (i.e. Unity's 3D Rigidbody component) 
    ObiRigidbody (i.e. the 3D version of ObiRigidbody)
    Box Collider (i.e. a 3D collider)
    Obi Collider (i.e. the 3D version of ObiCollider

But most importantly... All the errors go away!
Reply
#2
Hi,

Thanks for reporting this! I will take a look at it and patch it asap.

kind regards,
Reply