I'm facing serious problem with handling hinge joints dynamically in Unity.
I made bunch of chains and they are all linked for make something like "sealed door".
When I remove HingeJoint component at the runtime from Inspector, chains were unlinked and everything works fine.
But when I try same thing in the script, sometimes it works and sometimes not(And most of time doesn't work). And some condition just doesn't work.
There are two chain links so I need to remove two hinge joints, one named chain1 and one named chain2.
This is the code what am I tried:
Destroy(chain1.GetComponent<HingeJoint>());
Destroy(chain2.GetComponent<HingeJoint>());
Above code is just remove hinge joints but it doesn't worked always. So I added some delays:
IEnumerator RemoveHingeAfterTime(float delay, Rigidbody chainRB) {
yield return new WaitForSeconds(delay);
Destroy(chainRB.GetComponent<HingeJoint>());
}
...
StartCoroutine(RemoveHingeAfterTime(0.1f, chain1.GetComponent<Rigidbody>()));
StartCoroutine(RemoveHingeAfterTime(0.2f, chain2.GetComponent<Rigidbody>()));
Above code is just quite random, sometimes work sometimes not. Especially chain2 almost not works.
I adjusted time to fix this, but nothing works. "yield return null" or "yield return new WaitForFixedUpdate" also doesn't worked.
My doesn't worked means like this:
Also I toggled isKinematics and useGravity with same logic(just hope that physics works) but doesn't worked either.
I'm using Unity 5.6.1f1. Is this glitch? Is there a way to fix this?
Any advice will very appreciate it.