Two Game Objects with same transform.position but different location on Screen

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!

the only things I can think of are

otherGameObject.transform.position = transform.position

if that doesn’t work

then do you maybe have a rigidbody with position locks on the other game object?
(Cause I did that once)… :frowning:

Hello Rick-Gamez, thank you for your answer!

I have already tried two versions of your suggested code, one is exactly like you wrote, and the other one was assigning the coordinates to variables which i used in the new Vector3() constructor:

int x = transform.position.x;
int y = transform.position.y;
int z = transform.position.z;

newGameObject.transform.position = new Vector3(x,y,z,);

However, which version I use, I end up with the same result.

And I don’t have any rigidbody attached to any of the game objects. [However, the new GameObject has one component only: A Sprite Renderer, but this should not be relevant in this case if I do not oversee something.]

Hope that push is ok because I updated my original question with important details…

This may be off base, but on your tool settings, do you have it set to Pivot or Center? The latter can produce results like this.

7 Likes

You are right! When it’s on “Center” I have the behaviour described above, when I switch to “Pivot” the position marker is exactly at the same position as the other Game Object, which is what I actually expect to see.

That helped! So I have just to find out what Center/Pivot means and what it has to do with my new Game Object Sprite position :slight_smile: Many thanks!

5 Likes

I solved the problem. I needed to change the Pivot parameter in my Sprite.Create call to “new Vector2(0f, 0f)” and additionally change the Pivot in the Sprite Import Settings to “Bottom Left”.

Many thanks for your input!

1 Like