- Home /
Using DestroyImmediate on one GameObject makes a reference to another a null
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
try SetActive(false); then delete the whole thing after.
This only traverses in one direction, right? So what happens when you click in the middle of the rope?
currenly it removes only one link wherever i click, but if it worked correctly, it would simply traverse from that link up and remove half of the rope. If I wanted to remove the whole rope, I would have to click at its end
Answer by foxt451 · Mar 22, 2021 at 01:26 PM
Well, it was actually very easy. I confused connectedBody
with attachedRigidbody
, the second one apparently refers to the same object.
Your answer
Follow this Question
Related Questions
Deleted object tagged as missing instead of null. 0 Answers
Null Reference Problem Code+pic (SOLVED) 2 Answers
ScriptableObject resets on Play 0 Answers
Null Reference Exception error 3 Answers