Radu Mitroi
Radu Mitroi

Reputation: 41

Changing the parent of an instantiated object changes its position, how do I keep it at the instantiated position

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;

Upvotes: 1

Views: 3178

Answers (3)

Radu Mitroi
Radu Mitroi

Reputation: 41

I found out what my problem was. It was another line of code that I was using to spawn these prefabs in a grid-like manner and I was messing with the instantiated object's local position after I was setting the new parent. Putting the line of code for setting the parent after messing with the local position solved the issue.

Upvotes: 1

Immersive
Immersive

Reputation: 1704

Newly instantiated objects have a default position of local-0,0,0. If you provide them with a parent in the call to Instantiate, they are created at the location of the parent object (If you don't parent them until after then they will spawn at world-0,0,0).

You never actually provide any modification to the position. Even this snippet,

    Vector3 originalPos = spawningZone.transform.position;
    GameObject theTile = Instantiate(thePrefab, spawningZone.transform);
    theTile.transform.SetParent(parentObj.transform);
    theTile.transform.position = originalPos;

sets the position of theTile to be identical to the position of spawningZone.

(In case you weren't aware, instantiating a prefab ignores the position and rotation of the prefab root object)

Upvotes: 0

AlexWei
AlexWei

Reputation: 1103

GameObject theTile = Instantiate(thePrefab, spawningZone.transform);
theTile.transform.SetParent(parentObj.transform, true);

public void SetParent(Transform parent, bool worldPositionStays);

The 2nd para should be true. So it will stay where it was.

le.transform.position = originalPos;

Upvotes: 3

Related Questions