Position Issues

I had an issue with the position of the cloned prefab(canvas) being way off. Here’s the updated code used to fix the issue:

GameObject CanvasClone = Instantiate(enemyCanvasPrefab, transform.position, Quaternion.identity, transform);
        print(transform.position);
        print(this.transform.position);
        CanvasClone.transform.position = this.transform.position;

transform.position and this.transform.position are the same, yet when I try to instantiate it without a gameobject the cloned prefab ends up with some crazy position relative to the UI socket.

The issue has been resolved, but I’d just like to know why just instantiating-- vs setting it to a gameobject and changing the position-- doesnt work, if anyone knows.

Hi, I think the issue comes from this parameter:

instantiateInWorldSpace
Pass true when assigning a parent Object to maintain the world position of the Object, instead of setting its position relative to the new parent. Pass false to set the Object’s position relative to its new parent.

https://docs.unity3d.com/ScriptReference/Object.Instantiate.html

When you instantiate you are setting the position locally relative to the parent instead of the World, that’s why it shows up somewhere unexpected.

Hope it helps.

1 Like

You may want to check the prefab itself. I ran into this issue and noticed that the prefab does not have a 0, 0, 0 position when I imported it so it was throwing things off. Just make sure to reset the transform for the Enemy Canvas prefab and you should be good to go.

1 Like

Privacy & Terms