I want to instantiate an object to a given position, then change its parent to another gameobject, but keep the instantiated object in the same position I've instantiated it. Instead, the object changes its position to the object I'm parenting to.
I've got this empty GameObject the script is applied to. I am instantiating an object to another empty GameObject's position, that works fine. I then set the instantiated object to be a child of another empty GameObject, because I want to move the GameObject I've used as instantiating position without moving the instantiated object, because I want to keep instantiating that object to a new position. But that fails, the object I've generated moves itself to the position of the 3rd empty GameObject, the one I'm setting as a parent.
GameObject theTile = Instantiate(thePrefab, spawningZone.transform);
theTile.transform.SetParent(parentObj.transform);
After setting the new parent, the instantiated object moves itself to the parent's position. I've tried multiple ways of doing this, but for every method I've tried, the same thing was happening, the instantiated object moved to the new parent's position. These are the alternative methods I've tried:
GameObject theTile = Instantiate(thePrefab, spawningZone.transform);
theTile.transform.parent = transform.parent;
;
GameObject theTile = Instantiate(thePrefab, spawningZone.transform);
theTile.transform.SetParent(parentObj.transform, false);
;
Vector3 originalPos = spawningZone.transform.position;
GameObject theTile = Instantiate(thePrefab, spawningZone.transform);
theTile.transform.SetParent(parentObj.transform);
theTile.transform.position = originalPos;
theTile
to be identical to the parent,spawningZone
. – Immersive