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

The object of type "RigidBody" has been destroyed but you are still trying to acces it.

Discussion in 'Scripting' started by KoreanRice, Nov 14, 2017.

  1. KoreanRice

    KoreanRice

    Joined:
    Nov 11, 2017
    Posts:
    4
    I'm following a tutorial and I've come across an error after the player destroys an enemy ship.
    I added a new GameObject variable called enemy and I would only do what was in FixedUpdate if enemy wasn't null to try and fix this. It worked but only if there was one enemy on screen. If there were multiple, and I killed one I still got the error in the console. Here is the code for that

    Code (CSharp):
    1. public Boundary boundary;
    2.  
    3.     public float dodge;
    4.     public float smoothing;
    5.     public float tilt;
    6.  
    7.     public Vector2 startWait;
    8.     public Vector2 maneuverTime;
    9.     public Vector2 maneuverWait;
    10.  
    11.     public GameObject enemy;
    12.  
    13.     private float currentSpeed;
    14.     private float targetManeuver;
    15.     private static Rigidbody rb;
    16.  
    17.     void Start ()
    18.     {
    19.         StartCoroutine (Evade ());
    20.         rb = GetComponent <Rigidbody> ();
    21.         currentSpeed = rb.velocity.z;
    22.     }
    23.  
    24.     IEnumerator Evade ()
    25.     {
    26.         yield return new WaitForSeconds (Random.Range (startWait.x, startWait.y));
    27.  
    28.         while (true)
    29.         {
    30.             targetManeuver = Random.Range (1, dodge) * -Mathf.Sign (transform.position.x);
    31.             yield return new WaitForSeconds (Random.Range (maneuverTime.x, maneuverTime.y));
    32.             targetManeuver = 0;
    33.             yield return new WaitForSeconds (Random.Range (maneuverWait.x, maneuverWait.y));
    34.         }
    35.     }
    36.  
    37.     void FixedUpdate ()
    38.     {
    39.         if (enemy != null)
    40.         {
    41.             //I'm getting the error here
    42.             float newManeuver = Mathf.MoveTowards (rb.velocity.x, targetManeuver, Time.deltaTime * smoothing);
    43.             rb.velocity = new Vector3 (newManeuver, 0.0f, currentSpeed);
    44.             rb.position = new Vector3 (
    45.                 Mathf.Clamp (rb.position.x, boundary.xMin, boundary.xMax),
    46.                 0.0f,
    47.                 Mathf.Clamp (rb.position.z, boundary.zMin, boundary.zMax)
    48.             );
    49.        
    50.         rb.rotation = Quaternion.Euler (0.0f, 0.0f, rb.velocity.x * -tilt);
    51.         }
    52.     }
    53.  
    I don't know what to do to fix this so any help is appreciated.
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    What line in the code you posted is the line being complained about in the console? (EvasiveManeuver.cs line 47)

    Why is "rb" being declared as static?
     
  3. georgeq

    georgeq

    Joined:
    Mar 5, 2014
    Posts:
    662
    Most likely, you have this script attached to 2 or more GameObjects, and since "rb" is static the second GameObject overwrites the reference to the first Rigidbody, the third overwrites the reference to the second, and so on, so if you may have 10 enemy ship but a reference to only one rigid body, and that's the only one you're updating, so if you destroy the GameObject it belongs to, it is still being referenced by all other active GameObjects and that's why you get that error. Remove "static" form the "rb" declaration and you'll get rid of this message.
     
  4. KoreanRice

    KoreanRice

    Joined:
    Nov 11, 2017
    Posts:
    4
    That worked. Thank you.