I have a rope that consists of links, one attached to another. Each link (except for the first one) has a HingeJoint2D
component, in which the connectedRigidbody
field is set to the rigidbody of the previous link. I paint the rope with a custom brush that sets all connections automatically, but I’m trying to implement Erase
method so that when I click on one link, the whole rope gets erased. I’m trying to do it traversing the rope by accessing connectedRigidbody
and deleting links one by one:
public override void Erase(GridLayout gridLayout, GameObject brushTarget, Vector3Int position)
{
GameObject toErase = GetGameobjectInCell(gridLayout, position, brushTarget.transform);
while (toErase != null)
{
GameObject nextToErase = toErase.GetComponent<HingeJoint2D>()?.attachedRigidbody.gameObject;
Debug.Log(nextToErase);
DestroyImmediate(toErase);
Debug.Log(nextToErase);
toErase = nextToErase;
}
}
this is what the Debugger says:
And I wonder why after using DestroyImmediate on toErase, nextToErase becomes null