C# 检测一个关节不';我再也没有连接的身体了

C# 检测一个关节不';我再也没有连接的身体了,c#,unity3d,C#,Unity3d,我有一个动态生成的GameObjects列表,它们使用SpringJoint相互连接,之后我需要销毁一些GameObject,但与最近销毁的GameObject的SprintJoint连接仍保留在其他对象中,在Unity Inspector中,我可以看到,由于对象不再存在,连接的实体如预期一样丢失: foreach (GameObject cell in combined) { SpringJoint joint = cell.GetComponent<SpringJoint>

我有一个动态生成的
GameObjects
列表,它们使用
SpringJoint
相互连接,之后我需要销毁一些
GameObject
,但与最近销毁的
GameObject
SprintJoint连接仍保留在其他对象中,在Unity Inspector中,我可以看到,由于对象不再存在,连接的实体如预期一样丢失:

foreach (GameObject cell in combined)
{
    SpringJoint joint = cell.GetComponent<SpringJoint>();
    if (joint.connectedBody == null)
        Destroy(joint);
}

但是我试图从代码中检测到这一点,这样我就可以完全删除
SprintJoint
,我目前正在使用这一代码,但似乎
connectedBody
正在返回一个
刚体
,即使父对象不再存在:

foreach (GameObject cell in combined)
{
    SpringJoint joint = cell.GetComponent<SpringJoint>();
    if (joint.connectedBody == null)
        Destroy(joint);
}

检查连接的实体是否不再存在的正确方法是什么?

请尝试使用

foreach(组合中的变量单元格)
{
var joint=cell.GetComponent();
如果(!接头连接体)
{
破坏(联合);
}
}
请注意,
Destroy
不会立即使对象
null
!它只被标记为“死”,实际的删除被延迟,直到GC启动并实际“销毁”对象


另一方面,
bool
操作符已经在调用
Destroy
时将您的对象标记为无效。

您确定正在调用您的代码吗?我无法复制此问题。我使用的代码是
joint=gameObject.GetComponent();如果(joint.connectedBody为null){Debug.LogError(“销毁的关节”);Destroy(joint);}否则{Debug.Log(joint.connectedBody.name);}
Yes,实际上,我逐行调试了代码,
connectedBody
总是返回一个
RigidBody
,即使检查员说它丢失了。您可以发布包含此内容的整个脚本吗?在这里问这个问题可能会更幸运:
foreach (var cell in combined)
{
    var joint = cell.GetComponent<SpringJoint>();
    if(!joint.connectedBody)
    {
        Destroy(joint);
    }
}