- Home /
the object of type rigidbody has been destroyed
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
public class movement : MonoBehaviour
{
Rigidbody rigid;
private float zOffset = 20;
private void Start()
{
rigid = GetComponent<Rigidbody>();
SwipeDetector.OnSwipe += SwipeDetector_OnSwipe;
}
private void SwipeDetector_OnSwipe(SwipeData data)
{
Vector3[] positions = new Vector3[2];
positions[0] = Camera.main.ScreenToWorldPoint(new Vector3(data.StartPosition.x, 0, data.StartPosition.y));
positions[1] = Camera.main.ScreenToWorldPoint(new Vector3(data.EndPosition.x, 0, data.EndPosition.y));
Vector3 throwdir = (positions[1] - positions[0]).normalized;
throwdir.y = 0;
rigid.AddForce(-throwdir*750f,ForceMode.Acceleration); //error line
}
}
and this
public class SwipeDetector : MonoBehaviour
{
private Vector2 fingerDownPosition;
private Vector2 fingerUpPosition;
[SerializeField]
private bool detectSwipeOnlyAfterRelease = false;
[SerializeField]
private float minDistanceForSwipe = 20f;
public static event Action<SwipeData> OnSwipe = delegate { };
private void Update()
{
foreach (Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
fingerUpPosition = touch.position;
fingerDownPosition = touch.position;
}
if (!detectSwipeOnlyAfterRelease && touch.phase == TouchPhase.Moved)
{
fingerDownPosition = touch.position;
DetectSwipe();
}
if (touch.phase == TouchPhase.Ended)
{
fingerDownPosition = touch.position;
DetectSwipe();
}
}
}
private void DetectSwipe()
{
if (SwipeDistanceCheckMet())
{
if (IsVerticalSwipe())
{
var direction = fingerDownPosition.y - fingerUpPosition.y > 0 ? SwipeDirection.Up : SwipeDirection.Down;
SendSwipe(direction);
}
else
{
var direction = fingerDownPosition.x - fingerUpPosition.x > 0 ? SwipeDirection.Right : SwipeDirection.Left;
SendSwipe(direction);
}
fingerUpPosition = fingerDownPosition;
}
}
private bool IsVerticalSwipe()
{
return VerticalMovementDistance() > HorizontalMovementDistance();
}
private bool SwipeDistanceCheckMet()
{
return VerticalMovementDistance() > minDistanceForSwipe || HorizontalMovementDistance() > minDistanceForSwipe;
}
private float VerticalMovementDistance()
{
return Mathf.Abs(fingerDownPosition.y - fingerUpPosition.y);
}
private float HorizontalMovementDistance()
{
return Mathf.Abs(fingerDownPosition.x - fingerUpPosition.x);
}
private void SendSwipe(SwipeDirection direction)
{
SwipeData swipeData = new SwipeData()
{
Direction = direction,
StartPosition = fingerDownPosition,
EndPosition = fingerUpPosition
};
OnSwipe(swipeData);
}
}
public struct SwipeData
{
public Vector2 StartPosition;
public Vector2 EndPosition;
public SwipeDirection Direction;
}
public enum SwipeDirection
{
Up,
Down,
Left,
Right
}
Answer by panim0_unity · Mar 09, 2020 at 03:14 PM
What's happening is that with this line:
SwipeDetector.OnSwipe += SwipDetector_OnSwipe;
We'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.
void OnEnable() {
SwipeDetector.OnSwipe += SwipDetector_OnSwipe;
}
void OnDisable() {
SwipeDetector.OnSwipe -= SwipDetector_OnSwipe;
}
****Thanks to StarManta from unity forums. This solved everything.
Answer by tormentoarmagedoom · Mar 09, 2020 at 02:14 PM
Hello,
In the error log, it says wich line of the code is. You should commence by there, find by debugging which element has been destroyed, then investigate why you try to access something that is destroyed.
If changed scenes, maybe you need to re-assign the variable again (when scenes changes, all objects outside the "DontDestoryOnLoad" are destroyed.
yes they do but why does this destroy it? I mean when I play 1 round eveything is ok but when I reload the scene I get this error
Answer by eneIr · Mar 09, 2020 at 02:23 PM
The error is in neither scripts, it shows up because the rigidbody may had been destroyed when the game over and wasn't assigned when you reload the scene or something like that. Can you show your try again button script?
public void endGame(){ Scene$$anonymous$$anager.LoadScene(0); }
just this