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. Dismiss Notice

the object of type rigidbody has been destroyed

Discussion in 'Scripting' started by panim0_unity, Mar 9, 2020.

  1. panim0_unity

    panim0_unity

    Joined:
    Nov 13, 2018
    Posts:
    47
    Hi, I checked other threads but could not find an answer. I get this error when I load the same scene like with try again button. error shows 2 scripts that has the possible erron but I cant figure it out please help

    Code (CSharp):
    1. public class movement : MonoBehaviour
    2. {
    3.     Rigidbody rigid;
    4.     private float zOffset = 20;
    5.  
    6.     private void Start()
    7.     {
    8.         rigid = GetComponent<Rigidbody>();
    9.         SwipeDetector.OnSwipe += SwipeDetector_OnSwipe;
    10.     }
    11.  
    12.     private void SwipeDetector_OnSwipe(SwipeData data)
    13.     {
    14.         Vector3[] positions = new Vector3[2];
    15.         positions[0] = Camera.main.ScreenToWorldPoint(new Vector3(data.StartPosition.x, 0, data.StartPosition.y));
    16.         positions[1] = Camera.main.ScreenToWorldPoint(new Vector3(data.EndPosition.x, 0, data.EndPosition.y));
    17.         Vector3 throwdir = (positions[1] - positions[0]).normalized;
    18.         throwdir.y = 0;
    19.             rigid.AddForce(-throwdir*750f,ForceMode.Acceleration); //error line
    20.      
    21.     }
    22. }
    and this

    Code (CSharp):
    1. public class SwipeDetector : MonoBehaviour
    2. {
    3.     private Vector2 fingerDownPosition;
    4.     private Vector2 fingerUpPosition;
    5.  
    6.     [SerializeField]
    7.     private bool detectSwipeOnlyAfterRelease = false;
    8.  
    9.     [SerializeField]
    10.     private float minDistanceForSwipe = 20f;
    11.  
    12.     public static event Action<SwipeData> OnSwipe = delegate { };
    13.  
    14.     private void Update()
    15.     {
    16.         foreach (Touch touch in Input.touches)
    17.         {
    18.             if (touch.phase == TouchPhase.Began)
    19.             {
    20.                 fingerUpPosition = touch.position;
    21.                 fingerDownPosition = touch.position;
    22.             }
    23.  
    24.             if (!detectSwipeOnlyAfterRelease && touch.phase == TouchPhase.Moved)
    25.             {
    26.                 fingerDownPosition = touch.position;
    27.                 DetectSwipe();
    28.             }
    29.  
    30.             if (touch.phase == TouchPhase.Ended)
    31.             {
    32.                 fingerDownPosition = touch.position;
    33.                
    34.                 DetectSwipe();
    35.             }
    36.         }
    37.     }
    38.  
    39.     private void DetectSwipe()
    40.     {
    41.         if (SwipeDistanceCheckMet())
    42.         {
    43.             if (IsVerticalSwipe())
    44.             {
    45.                 var direction = fingerDownPosition.y - fingerUpPosition.y > 0 ? SwipeDirection.Up : SwipeDirection.Down;
    46.                 SendSwipe(direction);
    47.             }
    48.             else
    49.             {
    50.                 var direction = fingerDownPosition.x - fingerUpPosition.x > 0 ? SwipeDirection.Right : SwipeDirection.Left;
    51.                 SendSwipe(direction);
    52.             }
    53.             fingerUpPosition = fingerDownPosition;
    54.         }
    55.     }
    56.  
    57.     private bool IsVerticalSwipe()
    58.     {
    59.         return VerticalMovementDistance() > HorizontalMovementDistance();
    60.     }
    61.  
    62.     private bool SwipeDistanceCheckMet()
    63.     {
    64.         return VerticalMovementDistance() > minDistanceForSwipe || HorizontalMovementDistance() > minDistanceForSwipe;
    65.     }
    66.  
    67.     private float VerticalMovementDistance()
    68.     {
    69.         return Mathf.Abs(fingerDownPosition.y - fingerUpPosition.y);
    70.     }
    71.  
    72.     private float HorizontalMovementDistance()
    73.     {
    74.         return Mathf.Abs(fingerDownPosition.x - fingerUpPosition.x);
    75.     }
    76.  
    77.     private void SendSwipe(SwipeDirection direction)
    78.     {
    79.         SwipeData swipeData = new SwipeData()
    80.         {
    81.             Direction = direction,
    82.             StartPosition = fingerDownPosition,
    83.             EndPosition = fingerUpPosition
    84.         };
    85.         OnSwipe(swipeData);
    86.     }
    87. }
    88.  
    89. public struct SwipeData
    90. {
    91.     public Vector2 StartPosition;
    92.     public Vector2 EndPosition;
    93.     public SwipeDirection Direction;
    94. }
    95.  
    96. public enum SwipeDirection
    97. {
    98.     Up,
    99.     Down,
    100.     Left,
    101.     Right
    102. }
     
  2. gaglabs

    gaglabs

    Joined:
    Oct 17, 2019
    Posts:
    185
    We would need to see the error is giving you to better understand. As far as i can see, this code shouldnt throw that error.
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    What's happening is that with this line:
    Code (csharp):
    1. SwipeDetector.OnSwipe += SwipDetector_OnSwipe;
    You're setting up SwipeDetector to call a method on object X. Then when you reload the scene, object X is destroyed, and identical object Y replaces it, but SwipeDetector is still trying to call the method on object X.

    A common practice to avoid this is to make sure the script cleans up after itself by using OnEnable/OnDisable instead.

    Code (csharp):
    1. void OnEnable() {
    2. SwipeDetector.OnSwipe += SwipDetector_OnSwipe;
    3. }
    4. void OnDisable() {
    5. SwipeDetector.OnSwipe -= SwipDetector_OnSwipe;
    6. }
     
    ilicstefan0307 and panim0_unity like this.
  4. panim0_unity

    panim0_unity

    Joined:
    Nov 13, 2018
    Posts:
    47
    saved my day, thanks a lot for explaination. respect