- Home /
SetParent(null, true) Does not work
Simple script that allows an object to move with the car while on top of it but moves independent again when it falls/jumps off.
When the object enters the trigger it works as intended, however when it exits it changes location (25, 5, 8 relitive to object --> 25, 5, 8 in world position) even though worldPositionStays is set to false. any ideas on how to solve the problem?
public class AttachObject : MonoBehaviour
{
public GameObject Car;
private void OnTriggerEnter(Collider other)
{
other.gameObject.transform.SetParent(Car.transform, true);
}
private void OnTriggerExit(Collider other)
{
other.gameObject.transform.SetParent(null, true);
}
}
Answer by Bunny83 · Apr 01, 2021 at 02:27 AM
The SetParent method has been added later specifically for the possibility to keep the local position. If you just set the parent property of the transform component the object will keep its worldspace position. This was always the case, even 10+ years ago. So doing
other.transform.parent = null;
should unparent the object and it should keep its worldspace position.
Your answer
![](https://koobas.hobune.stream/wayback/20220613031548im_/https://answers.unity.com/themes/thub/images/avi.jpg)