Hello,
In a script attached to a game object, I create another game object.
The new game object has no relation to the existing one (I do not set the transform.parent property).
Both game objects are on the same top level in the hierarchy, having no parents.
I set the transform.position of the new game object to the transform.position values of the existing game object via transform.position = new Vector3().
After that, I write out the .position and .localPosition values of both game objects to the log.
They have exactly the same values for both local and world position. In the Inspector, they have the same transform.position values.
However, in the game / scene view, the created game object is placed far away from the existing game object which is absolutely weird.
EDIT: Here are two screenshots to illustrate the problem:
You can see in the first picture I select the First Game Object. Look at the position. The coordinates are the same as the coordinates of the new Game Object selected in the second Picture.
But their positions in the world is completely different.
Here is the script attached to Game Object “HealthBar”:
using UnityEngine;
namespace Assets.Scripts.Boss
{
public class HealthBar : MonoBehaviour
{
public int Value;
private int _oldValue;
private GameObject _healthbarLife;
// Use this for initialization
void Start ()
{
_healthbarLife = GameObject.Find("HealthbarLife");
_oldValue = Value;
}
// Update is called once per frame
void Update ()
{
if (Value != _oldValue)
{
_oldValue = Value;
int originalSpriteWidth = _healthbarLife.GetComponent<SpriteRenderer>().sprite.texture.width;
int originalSpriteHeight = _healthbarLife.GetComponent<SpriteRenderer>().sprite.texture.height;
var originalSpriteRect = _healthbarLife.GetComponent<SpriteRenderer>().sprite.rect;
var destinationTexture = new Texture2D(originalSpriteWidth, originalSpriteHeight, TextureFormat.ARGB32, false);
Color[] sourcePixels = _healthbarLife.GetComponent<SpriteRenderer>().sprite.texture.GetPixels(0);
Color[] destinationPixels = destinationTexture.GetPixels(0);
for (int i = 0; i < destinationPixels.Length; ++i)
destinationPixels[i].a = 0.0f;
int xEnd = originalSpriteWidth - (40);
for (int y = 0; y < originalSpriteHeight; ++y)
{
for (int x = 0; x < xEnd; ++x)
{
int index = (y * originalSpriteWidth + x);
destinationPixels[index] = sourcePixels[index];
}
}
destinationTexture.SetPixels(destinationPixels, 0);
destinationTexture.Apply();
var go = new GameObject();
go.AddComponent<SpriteRenderer>();
go.GetComponent<SpriteRenderer>().sprite = Sprite.Create(destinationTexture, originalSpriteRect, _healthbarLife.GetComponent<SpriteRenderer>().sprite.pivot, 32);
go.GetComponent<SpriteRenderer>().sortingOrder = 0;
go.GetComponent<SpriteRenderer>().sortingLayerName = "Foreground";
//go.transform.parent = gameObject.transform;
go.transform.position = gameObject.transform.position;
Destroy(_healthbarLife);
Debug.Log(go.transform.position);
Debug.Log(go.transform.localPosition);
Debug.Log(transform.position);
Debug.Log(transform.localPosition);
}
}
}
}
Any feedback highly appreciated!

