Search Unity

MissingReferenceException...

Discussion in 'Physics' started by dogsta, May 23, 2017.

  1. dogsta

    dogsta

    Joined:
    May 15, 2016
    Posts:
    11
    Hi guys,

    Hopefully someone may shed some light here. I usually like to work things out for myself, but this one has me flummoxed.

    Full error in console:
    MissingReferenceException: The object of type 'FixedJoint' 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.
    destroyPlatform.collapse () (at Assets/Scripts/destroyPlatform.cs:50)
    destroyPlatform.OnTriggerStay (UnityEngine.Collider other) (at Assets/Scripts/destroyPlatform.cs:21)


    Basically I've created a floating collapsible platform. But I've noticed this error flags in the console for a dozen cycles or so, then clears. Doesn't seem to affect things as it runs. *scratches head*

    I'll drop the code in as it's probably easier than typing away an explanation.

    Forgive me if the code is ugly, I've not been at this long! *grins*


    Code (CSharp):
    1. public class destroyPlatform : MonoBehaviour {
    2.  
    3.     public float delay = 1f;
    4.  
    5.     private bool steppedOn = false;
    6.     private float timer = 0f;
    7.  
    8.     public Rigidbody rb1, rb2, rb3;
    9.     public FixedJoint fj1, fj2;
    10.  
    11.  
    12.     void OnTriggerStay(Collider other)
    13.     {
    14.         if (other.tag == "Player")
    15.         {
    16.             steppedOn = true;
    17.             collapse();
    18.         }
    19.     }
    20.  
    21.     private void OnTriggerExit(Collider other)
    22.     {
    23.         if (other.tag == "Player")
    24.         {
    25.             steppedOn = false;
    26.          
    27.         }
    28.     }
    29.  
    30.     void collapse()
    31.     {
    32.         if (steppedOn)
    33.         {
    34.             timer += Time.deltaTime;
    35.             if (timer > delay)
    36.             {
    37.                 rb1.useGravity = true;
    38.                 rb1.isKinematic = false;
    39.  
    40.                 rb2.useGravity = true;
    41.                 rb2.isKinematic = false;
    42.  
    43.                 rb3.useGravity = true;
    44.                 rb3.isKinematic = false;
    45.  
    46.                 fj1.breakForce = 0.1f;
    47.                 fj1.breakTorque = 0.2f;
    48.  
    49.                 fj2.breakForce = 0.1f;
    50.                 fj2.breakTorque = 0.2f;
    51.             }
    52.          
    53.  
    54.         }
    55.     }
    56. }
     
    Last edited: May 23, 2017
  2. dogsta

    dogsta

    Joined:
    May 15, 2016
    Posts:
    11
    Duh, don't worry. I've sussed it.

    It's every time the player character collides with the platform objects!

    But, if there's a way around it, I'd appreciate it.